Post processing of a Json response in spring MVC

后端 未结 1 1828
心在旅途
心在旅途 2020-12-08 11:31

I have several controllers that return the same generic Response object with @ResponseBody annotation, like this:

@RequestMapping(value = \"/status\", method         


        
1条回答
  •  感动是毒
    2020-12-08 11:43

    In the end I implemented ResponseBodyAdvice like this:

    @ControllerAdvice
    public class StatusAdvice implements ResponseBodyAdvice> {
    
    
        @Override
        public boolean supports(MethodParameter returnType,
                Class> converterType) {
    
            if (returnTypeIsReponseVM(returnType)&&responseConverterIsJackson2(converterType)){
                return true;
            }
    
            return false;
        }
    
    ....
    
        @Override
        public Response beforeBodyWrite(Response body, MethodParameter returnType,
                MediaType selectedContentType,
                Class> selectedConverterType,
                ServerHttpRequest request, ServerHttpResponse response) {
    
            ....
    
            return body;
        }
    
    }
    

    So it was easier then expected.

    0 讨论(0)
提交回复
热议问题