在spring中使用RequestBodyAdvice 和 ResponseBodyAdvice增强器

阅读数:69 评论数:0

跳转到新版页面

分类

python/Java

正文

一、概述

RequestBodyAdvice 和 ResponseBodyAdvice来对请求前后进行处理,本质上他俩都是AOP拦截器。
  
RequestBodyAdvice 和 ResponseBodyAdvice都需要搭配@RestControllerAdvice或@ControllerAdvice使用。

二、RequestBodyAdvice

往往需要对请求参数做一些统一的操作 , 例如参数的过滤 , 字符的编码 , 第三方的解密等等 , Spring提供了RequestBodyAdvice一个全局的解决方案 , 免去了我们在Controller处理的繁琐。

1、作用范围

RequestBodyAdvice仅对使用了@RequestBody注解的生效 , 因为它原理上还是AOP , 所以GET方法是不会操作的。

它可将处理结果对象作为@RequestBody参数或者 @HttpEntity方法参数。

2、提供的方法

(1)该方法返回true时,才会进去下面的系列方法

// 是否开启拦截 true开启 false不开启
boolean supports(MethodParameter methodParameter, Type targetType,
		Class<? extends HttpMessageConverter<?>> converterType);
}

(2)body数据读取之前调用,一般在此方法中对body数据进行修改

 //请求体解析前处理
HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,
		Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException;

(3)body读取之后操作,一般直接返回原实例

//请求体解析后处理
Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,
		Type targetType, Class<? extends HttpMessageConverter<?>> converterType);

(4)当body为empty时操作

//处理没有参数
@Nullable
Object handleEmptyBody(@Nullable Object body, HttpInputMessage inputMessage, MethodParameter parameter,
		Type targetType, Class<? extends HttpMessageConverter<?>> converterType);

3、实例

当控制层方法或者类上有标记注解 @RequestAdvice注解时,才会进入其他相关方法。当不需要任何限制时,supports直接返回true即可。

@Slf4j
@ControllerAdvice
public class RequestInterceptor implements RequestBodyAdvice {
	 // 是否开启拦截 true开启 false不开启
    @Override
    public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
    	// 方法上是否有RequestAdvice注解
        RequestAdvice requestAdvice = methodParameter.getMethodAnnotation(RequestAdvice.class);
        if (requestAdvice == null) {
        	// 类上是否有RequestAdvice注解
            requestAdvice = methodParameter.getDeclaringClass().getAnnotation(RequestAdvice.class);
        }
        return requestAdvice != null;
    }

	//请求体解析前处理
    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {
		 // 常见的业务仓场景有: 1 记录日志 2 内容加密解密 3 是否开启分页功能
        log.info("拦截到的请求参数为 = {}",methodParameter.toString());
        Method method=methodParameter.getMethod();
        log.info("请求体读取前={}==>{}==>{}==>{}",method.getDeclaringClass().getSimpleName(),method.getName(),type.toString(),aClass.getName());
		
		//return new XHttpInputMessage(httpInputMessage, "UTF-8");
		// 设置utf8编码
        String bodyStr = IOUtils.toString(httpInputMessage.getBody(), StandardCharsets.UTF_8);
        return new HttpInputMessage() {
            @Override
            public InputStream getBody() throws IOException {
                ApiRequest<Object> request = JsonUtils.json2Obj(bodyStr, new TypeReference<ApiRequest<Object>>() {
                });
                String body = bodyStr;
                if (request != null && request.getData() != null) {
                    body = JsonUtils.obj2Json(request.getData());
                }
                return new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8));
            }

            @Override
            public HttpHeaders getHeaders() {
                return httpInputMessage.getHeaders();
            }
        };
    }

	//请求体解析后处理
    @Override
    public Object afterBodyRead(Object body, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
     	Method method=methodParameter.getMethod();
        log.info("请求体读取后={}.{}:{}",method.getDeclaringClass().getSimpleName(),method.getName(),o.toString());
        return body;
    }

	//处理没有参数
    @Override
    public Object handleEmptyBody(Object body, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
     	Method method=methodParameter.getMethod();
        log.info("没有参数={}.{}",method.getDeclaringClass().getSimpleName(),method.getName());
        return body;
    }
}

三、ResponseBodyAdvice

ResponseBodyAdvice可以在注解@ResponseBody将返回值处理成相应格式之前操作返回值。

1、作用范围

它允许在 执行 @ResponseBody后自定义返回数据,或者将返回@ResponseEntity的 Controller Method在写入主体前使用HttpMessageConverter进行自定义操作。

2、提供的方法

(1)该方法返回true时,才会进去下面的 beforeBodyWrite方法。

boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);

(2)body写入前的操作

@Nullable
T beforeBodyWrite(@Nullable T body, MethodParameter returnType, MediaType selectedContentType,
		Class<? extends HttpMessageConverter<?>> selectedConverterType,
		ServerHttpRequest request, ServerHttpResponse response);

3、实例

@RestControllerAdvice
public class BaseResponseBodyAdvice implements ResponseBodyAdvice<Object> {


    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
         ResponseAdvice responseAdvice = returnType.getMethodAnnotation(ResponseAdvice.class);
        if (responseAdvice == null) {
            responseAdvice = returnType.getDeclaringClass().getAnnotation(ResponseAdvice.class);
        }
        return responseAdvice != null;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType,
            MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request,
            ServerHttpResponse response) {

        // 遇到feign接口之类的请求, 不应该再次包装,应该直接返回
        // 上述问题的解决方案: 可以在feign拦截器中,给feign请求头中添加一个标识字段, 表示是feign请求
        // 在此处拦截到feign标识字段, 则直接放行 返回body.

        System.out.println("响应拦截成功");
		
		//如果直接响应字符串返回,则会报类型转换异常.
        if(body instanceof String){
            return JSONUtil.toJsonStr(BaseResponse.ok(o));
        }
        if (body instanceof BaseResponse) {
            return body;
        } else if (body == null) {
            return BaseResponse.ok();
        } else {
            return BaseResponse.ok(body);
        }
    }
}



相关推荐

一、spring5之前的springMVC异常处理机制 1、使用@ExceptionHandler 用于局部方法捕获,与抛出异常的方法处于同一个Controller类。 @Controller pub