|
@@ -11,10 +11,13 @@ import org.springframework.web.bind.annotation.RestControllerAdvice; |
|
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; |
|
|
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* @Author liuxinxin |
|
|
|
|
|
* @Date 2021/7/21 11:26 |
|
|
|
|
|
* @Version 1.0 |
|
|
|
|
|
**/ |
|
|
|
|
|
|
|
|
* <p> |
|
|
|
|
|
* GlobalResponseHandler |
|
|
|
|
|
* </p> |
|
|
|
|
|
* |
|
|
|
|
|
* @author WendyYang |
|
|
|
|
|
* @since 10:29 2023/12/29 |
|
|
|
|
|
*/ |
|
|
@RestControllerAdvice(basePackages = { |
|
|
@RestControllerAdvice(basePackages = { |
|
|
"com.hz.pm.api.fiscal.controller", |
|
|
"com.hz.pm.api.fiscal.controller", |
|
|
"com.hz.pm.api.organization.controller", |
|
|
"com.hz.pm.api.organization.controller", |
|
@@ -37,6 +40,7 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; |
|
|
"com.hz.pm.api.gov.controller", |
|
|
"com.hz.pm.api.gov.controller", |
|
|
"com.hz.pm.api.dashboard.controller", |
|
|
"com.hz.pm.api.dashboard.controller", |
|
|
"com.hz.pm.api.wps.controller", |
|
|
"com.hz.pm.api.wps.controller", |
|
|
|
|
|
"com.hz.pm.api.external.controller", |
|
|
"com.hz.pm.api.gov.controller" |
|
|
"com.hz.pm.api.gov.controller" |
|
|
}) |
|
|
}) |
|
|
public class GlobalResponseHandler implements ResponseBodyAdvice<Object> { |
|
|
public class GlobalResponseHandler implements ResponseBodyAdvice<Object> { |
|
@@ -52,16 +56,7 @@ public class GlobalResponseHandler implements ResponseBodyAdvice<Object> { |
|
|
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, |
|
|
public Object beforeBodyWrite(Object o, MethodParameter methodParameter, MediaType mediaType, |
|
|
Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, |
|
|
Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, |
|
|
ServerHttpResponse serverHttpResponse) { |
|
|
ServerHttpResponse serverHttpResponse) { |
|
|
ApiResponse<Object> apiResponse = ApiResponse.ofSuccess(o); |
|
|
|
|
|
// 处理字符串时,遇到了类型转换的问题,debug一步一步跟踪,原来是对于字符串的ContentType是“text-plain”, |
|
|
|
|
|
// ConverterType是StringHttpMessageConverter这个类型转换, |
|
|
|
|
|
// 由于将结果封装成了自定义的ApiResponse类型,所以有ApiResponse转换成String报错 |
|
|
|
|
|
// 所以需要对String类型的返回值单独进行处理 |
|
|
|
|
|
// if (o instanceof String) { |
|
|
|
|
|
// return JSONUtil.toJsonStr(apiResponse); |
|
|
|
|
|
// } |
|
|
|
|
|
return ApiResponse.ofSuccess(o); |
|
|
return ApiResponse.ofSuccess(o); |
|
|
// return o; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private Boolean filter(MethodParameter methodParameter) { |
|
|
private Boolean filter(MethodParameter methodParameter) { |
|
|