当前位置:   article > 正文

SpringBoot统一接口返回_springboot 返回统一的code massage

springboot 返回统一的code massage

前言:

前后分离时,我们要定义好统一的接口返回格式
eg:{“code”:0 ,“message”:“成功”,“data”:null}
返回格式与业务不相关且重复构建结果会产生大里冗余代码, 那么该怎么去抽离出来?
可以考虑将包含某些标记的方法或者类的结果进行重写,我们可以使用ResponseBodyAdvice来实现,当然也可作为加密解密的一种实现方式

思路

1.定义标识,可以定义一个注解作为标识

@Target({ElementType.TYPE,ElementType.METHOD})
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface WebResponseResultAdvice {

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

2.对Controller或者method打上标识

  @RequestMapping("/webResultAdvice")
    @WebResponseResultAdvice
    public Object testWebResult(String param) {
        return demoService.testGlobalResult(param);
    }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

3.请求时判断是否存在该标识,可以利用拦截器

public class ResponseResultInterceptor extends HandlerInterceptorAdapter {
    private final static String WEB_RESULT_BUILD_TAG = "webResultBuildTag";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (handler instanceof HandlerMethod) {
            HandlerMethod handlerMethod = (HandlerMethod)handler;
            //获取class
            Class<?> classType = handlerMethod.getBeanType();
            //获取method
            Method method = handlerMethod.getMethod();
            if (classType.isAnnotationPresent(WebResponseResultAdvice.class) ||
                    method.isAnnotationPresent(WebResponseResultAdvice.class)) {
                request.setAttribute(WEB_RESULT_BUILD_TAG,true);
            }
        }
        return true;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

springboot可以统一配置在mvcconfig中

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        InterceptorRegistration interceptorRegistration = registry.addInterceptor(new ResponseResultInterceptor());
        interceptorRegistration.addPathPatterns("/**");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

4.对结果重新写入

@ControllerAdvice
public class BaseWebResultHandler implements ResponseBodyAdvice {
    private final static String WEB_RESULT_BUILD_TAG = "webResultBuildTag";

    @Override
    public boolean supports(MethodParameter methodParameter, Class aClass) {
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = requestAttributes.getRequest();
        Object attribute = request.getAttribute(WEB_RESULT_BUILD_TAG);
        return attribute == null ? false:true;
    }

    @Override
    public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, Class aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        return JSON.toJSONString(WebResultDto.buildSuccessResult(o));
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/183985?site
推荐阅读
相关标签
  

闽ICP备14008679号