0%

RestControllerAdvice返回String时异常

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35


@RestControllerAdvice(value = "cn.xx996")
public class CommonResponseDataAdvice implements ResponseBodyAdvice<Object> {
@Override
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
return true;
}

@Override
public Object beforeBodyWrite(Object o, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse serverHttpResponse) {

CommonResponse<Object> response = new CommonResponse<>("", 0);

if (null == o) {
return response;
} else if (o instanceof CommonResponse) {
response = (CommonResponse<Object>) o;
} else if (o instanceof String) {

//返回是String类型时,单独处理。转换成JSON字符串即可
response.setData(o);
return JSON.toJSONString(response);
} else {
response.setData(o);
}

return response;

}
}