Spring Boot 返回加密后的 Response
Spring Boot About 1,091 wordsResponseBodyAdvice
实现ResponseBodyAdvice
接口,在supports()
方法中返回true
会进入beforeBodyWrite()
,在beforeBodyWrite()
中对Controller
中返回的对象Object body
进行修改。
下面示例中的Response
为自定义类。
@RestControllerAdvice
@Order(Ordered.HIGHEST_PRECEDENCE)
public class ApiResponseBodyAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body instanceof Response || body instanceof ProblemDetail) {
return body;
}
body = Response.builder().data(body).build();
return body;
}
}
Filter
在doFilter()
的FilterChain
调用filterChain.doFilter(request, response);
之后再进行对HttpServletResponse
读取流,进行修改。
Interceptor
在postHandle()
或afterCompletion()
方法中读取HttpServletResponse
中的流进行修改。
Views: 1,250 · Posted: 2023-01-28
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...