使用 @ControllerAdvice & ResponseBodyAdvice 拦截Controller方法默认返回参数,统一处理返回值/响应体


1、Controller代码

以下是Controller查询方法源码:


/**
* controller
*
* @author yclimb
* @date 2018/6/29.
*/
@RestController
@RequestMapping("/manage/user")
public class TestController { private Logger logger = LoggerFactory.getLogger(TestController.class); /**
* 通过会员id获取会员信息及详情
*
* @param id 会员id
* @return app msg
*
* @author yclimb
* @date 2018/6/29
*/
@GetMapping("/edit/{id}")
public AppMessage edit(@PathVariable Integer id) {
if (null == id) {
return AppMessage.error(-3, "会员ID不能为空");
} // 查询会员对象
User user = userService.queryById(id);
if (user == null) {
return AppMessage.error(-3, "根据会员ID未查询到对应会员");
} return AppMessage.success(user);
} }

2、拦截处理器

下面已经封装好了一个拦截处理类以供参考:

package com.yclimb.test;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; /**
* 拦截Controller方法默认返回参数,统一处理返回值/响应体
*
* @author yclimb
* @date 2018/6/29
*/
@ControllerAdvice
public class TestResponseBodyAdvice implements ResponseBodyAdvice { @Override
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) { System.out.println("TestResponseBodyAdvice==>beforeBodyWrite:" + o.toString() + ","
+ methodParameter);
return o;
} @Override
public boolean supports(MethodParameter methodParameter, Class aClass) {
return true;
}
}

3、测试方法

Controller:

ResponseBodyAdvice:

结语

到此本文就结束了,关注公众号查看更多推送!!!



使用 ResponseBodyAdvice 拦截Controller方法默认返回参数,统一处理返回值/响应体的更多相关文章

  1. 利用Ajax调用controller方法并传递参数

    一.背景由于近期工作需要将人脸识别功能与选课系统结合,但是对前端知识了解的很少,只能边做边学了,因此在这边把遇到的一些坑说明一下,希望能帮助到像我一样的初学者 二.具体内容这里采用框架为MVC,如果想 ...

  2. 工作随笔——Java调用Groovy类的方法、传递参数和获取返回值

    接触Groovy也快一年了,一直在尝试怎么将Groovy引用到日常工作中来.最近在做一个功能的时候,花了点时间重新看了下Java怎么调用Groovy的方法.传递参数和获取返回值. 示例Groovy代码 ...

  3. SpringMVC基础-controller方法中的参数注解

    @PathVariable  映射 URL 绑定的占位符 带占位符的 URL 是 Spring3.0 新增的功能,该功能在 SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义 通过 ...

  4. 范型方法 & 范型参数 & 范型返回值

    Java范型类 public class FanXingClassTest { public static void main(String args[]){ Test<Integer> ...

  5. C++ 调用Python文件方法传递字典参数并接收返回值

    首先本地需要安装有Python环境,然后在c++工程中包含Python的头文件,引用Python的lib库. //python 初始化 Py_Initialize(); if (!Py_IsIniti ...

  6. SpringBoot统一处理返回结果和异常情况

    如果文章有帮助到你,还请点个赞或留下评论 原因 在springboot项目里我们希望接口返回的数据包含至少三个属性: code:请求接口的返回码,成功或者异常等返回编码,例如定义请求成功. messa ...

  7. SpringMVC的@RequestMapping和Controller方法返回值

    本节内容: @RequestMapping Controller方法返回值 一.@RequestMapping 通过@RequestMapping注解可以定义不同的处理器映射规则. 1. URL路径映 ...

  8. springmvc中Controller方法的返回值

    1.1 返回ModelAndView controller方法中定义ModelAndView对象并返回,对象中可添加model数据.指定view. 1.2 返回void 在controller方法形参 ...

  9. SprimgMVC学习笔记(五)—— Controller方法返回值

    一.返回ModelAndView controller方法中定义ModelAndView对象并返回,对象中可添加model数据.指定view. /** * 查询商品列表 * @return */ @R ...

随机推荐

  1. 什么是Docker并且它为什么这么受欢迎

    什么是Docker (why it's so hot than hot) Docker是一个使用容器来方便快捷的创建,部署,运行程序的工具,容器允许开发人员将应用程序的一切打包(镜像),例如库和其他的 ...

  2. Java入门:char与byte的区别

    byte 是字节数据类型 ,是有符号型的,占1 个字节:大小范围为-128—127 .char 是字符数据类型 ,是无符号型的,占2字节(Unicode码 ):大小范围 是0—65535 :char是 ...

  3. Ansible7:Playbook常用模块

    目录 template set_fact pause wait_for assemble add_host group_by debug fail playbook的模块与在ansible命令行下使用 ...

  4. python 中的queue, deque

    python3 deque(双向队列) 创建双向队列 import collections d = collections.deque() append(往右边添加一个元素) import colle ...

  5. python中__init__()、__new__()、__call__()、__del__()用法

    关于__new__()的用法参考: http://www.myhack58.com/Article/68/2014/48183.htm 正文: 一.__new__()的用法: __new__()是在新 ...

  6. MESS-配置

    Author:KillerLegend From:http://www.cnblogs.com/killerlegend/p/3824989.html Date:2014.7.4 Part A 第一部 ...

  7. [CQOI2009]DANCE跳舞(ISAP写法)

    https://daniu.luogu.org/problemnew/show/3153 #include<queue> #include<cstdio> #include&l ...

  8. 2017 清北济南考前刷题Day 6 afternoon

    期望得分:100+100+30=230 实际得分: 正解: 枚举最高的位,这一位m是1但实际用了0 然后剩余的低位肯定是 正数就用1,负数用0 考场思路:数位DP #include<cstdio ...

  9. negativeView 的使用

    参考链接:http://blog.csdn.net/u012702547/article/details/51253222 1.一般来讲,是配合drawerLayout使用的,在xml文件中声明,其中 ...

  10. List(JDK1.7)(2)

    LinkedList List接口和Deque接口的一种双向链表实现.非同步的. 快速失败机制.ConcurrentModificationException 结点结构 插入结点 删除结点 add() ...