使用@RestControllerAdvice()注解进行统一返回封装
@Slf4j
@RestControllerAdvice(basePackages = {"cyh.travel.controller", "cyh.user.controller","cyh.shop.controller","cyh.order.controller","cyh.alipay.controller"})
@SuppressWarnings("all")
public class CommonResponseAdvice implements ResponseBodyAdvice<Object> {
/**
* 判断是否需要进行统一返回处理
*
* @param returnType
* @param converterType
* @return
*/
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
if (returnType.hasMethodAnnotation(IgnoreResponseAdviceReturn.class) || returnType.hasParameterAnnotation(IgnoreResponseAdvice.class)) {
return false;
}
return true;
}
/**
* 进行统一返回处理
*
*/
@Override
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
if (body instanceof String) {
return body;
}
return ResultData.success("ok", body);
}
}
忽略全局封装返回
/**
* 忽略统一返回
*
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreResponseAdvicerReturn {
}