Affects: \<5.3.23>
I'm wrapping the return values of all rest-apis with ResponseBodyAdvice.
@RestControllerAdvice(basePackages = "io.transwarp.community.web")
public class ResponseAdvice implements ResponseBodyAdvice<Object> {
private final I18nService i18nService;
@Override
public boolean supports(
@NonNull MethodParameter returnType,
@NonNull Class<? extends HttpMessageConverter<?>> converterType) {
Method method = returnType.getMethod();
if (method != null && method.getAnnotation(NonWrap.class) != null) {
return false;
}
return !returnType.getParameterType().equals(Response.class);
}
@Override
public Object beforeBodyWrite(
Object body,
@NonNull MethodParameter returnType,
@NonNull MediaType selectedContentType,
@NonNull Class<? extends HttpMessageConverter<?>> selectedConverterType,
@NonNull ServerHttpRequest request,
@NonNull ServerHttpResponse response) {
return Response.ok(body);
}
}
@PostMapping("/login")
public void login(
@RequestBody LoginUserDto loginUserDto,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
userService.login(loginUserDto, request, response);
}
But when my api method has an argument of HttpServletResponse, and my api return type is void or return value is null, the response will not be wrapped by ResponseBodyAdvice.
Comment From: bclozel
This behavior is expected, as ResponseBodyAdvice
is only involved for the following cases:
- Allows customizing the response after the execution of an
@ResponseBody
- or a
ResponseEntity
controller method but before the body is written- with an
HttpMessageConverter
.
In Spring MVC, those are involved in the HttpEntityMethodProcessor
- the case you're showing here doesn't involve this mechanism and the controller handler is supposed to handle the response entirely.
For further questions, please use StackOverflow.