返回数据时,对数据进行统一处理ResponseBodyAdvice

@ControllerAdvice
public class VOHandlerAdvice implements ResponseBodyAdvice {

    @Override
    public boolean supports(MethodParameter returnType, Class converterType) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType,
        ServerHttpRequest request, ServerHttpResponse response) {
        VOWrapper VOWrapper = new VOWrapper();
        VOWrapper.setData(body);

        return VOWrapper;
    }

    @Data
    private static class VOWrapper {
        private Object data;
    }
}

上面实现:在返回数据时,最外层封装一层data,即原来为{"a":"11212"},处理后变为

{
    "data": 
    {
        "a":"11212"
    }
}

你可能感兴趣的:(笔记)