Skip to content

Latest commit

 

History

History

spring-restful-template-demo

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

统一返回值demo

通过实现ResponseBodyAdvice,并重写beforeBodyWrite方法

    /**
     * 返回值 responseBody 统一配置
     */
    @RestControllerAdvice
    static class CommonResultResponseAdvice 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 CommonResult) {
                return body;
            }
            return new CommonResult<Object>(body);
        }
    }

配置位置