Spring MVC 3.2 @ResponseBody interceptor

后端 未结 4 2129
北荒
北荒 2021-02-05 22:28

In our application we are using JSON for request and response. The controller methods are annotated with @RequestBody(). The object being returned e.g. TransferResponse. I would

4条回答
  •  梦谈多话
    2021-02-05 22:47

    Since the question was posted, ResponseBodyAdvice was added in Spring MVC 4.1. This interface allows applications to alter or completely change the body, before the converter is applied. The documentation for intercepting requests was also updated specifically for this issue:

    Note that the postHandle method of HandlerInterceptor is not always ideally suited for use with @ResponseBody and ResponseEntity methods. In such cases an HttpMessageConverter writes to and commits the response before postHandle is called which makes it impossible to change the response, for example to add a header. Instead an application can implement ResponseBodyAdvice and either declare it as an @ControllerAdvice bean or configure it directly on RequestMappingHandlerAdapter.

提交回复
热议问题