ResponseBodyService
2018-12-26 本文已影响0人
FynnLiu
简要描述:
- 跟前端约定的响应对象格式类似于:{"respCode":"0000","respMsg":"xxxx",respData:{业务对象}},对于统一响应的处理方案有以下几种,可以根据情况适当选择
example1:
所有返回对象都继承统一BaseResponse,父类中定义code,msg
此方案限制性比较强
example2:
统一一个工具类,所有controller返回值统一范型格式,最终通过工具类方法或抽象方法返回,
此方式代码过于模版化
public JsonResp<RepaymentListDetailResponse> getRepaymentDetail(@Param OrderNoRequest request){
return getSuccessResponse(resp);
}
example3:
自定义ResponseBodyService,统一处理返回对象,业务代码正常返回普通pojo
@ControllerAdvice("com.ly.fn.biz.ins.miniapp")
@Slf4j
@Component
public class ResponseBodyAdvice
implements org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType);
}
@SuppressWarnings("all")
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType,
return getSuccessResponse(body);
}