当前位置:网站首页>Error reporting caused by the use of responsebodyadvice interface and its solution

Error reporting caused by the use of responsebodyadvice interface and its solution

2022-07-23 18:49:00 Integer_ Double

One 、ResponseBodyAdvice Use

ResponseBodyAdvice Used before the return value is written to the response , take body Re encapsulation of content , Go straight to the code

1.1 CommonResponseDataAdvice

@Component
public class CommonResponseDataAdvice implements ResponseBodyAdvice<Object> {
    
    private static final String V_3_API_DOCS = "/v3/api-docs";
    private static final String SWAGGER_RESOURCES = "/swagger-resources";
    private static final String ADMIN_ACTUATOR = "/actuator";

    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
    
        if (methodParameter.getAnnotatedElement().isAnnotationPresent(IgnoreResponseAdvice.class)) {
    
            return false;
        } else {
    
            return !methodParameter.getMethod().isAnnotationPresent(IgnoreResponseAdvice.class);
        }
    }

    @Nullable
    public Object beforeBodyWrite(@Nullable Object o, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
    
        // Avoid being right swagger Of api The interface return value is encapsulated 
        if (((ServletServerHttpRequest) serverHttpRequest).getServletRequest().getRequestURI().equals(V_3_API_DOCS)) {
    
            return o;
        }
        if (((ServletServerHttpRequest) serverHttpRequest).getServletRequest().getRequestURI().contains(SWAGGER_RESOURCES)) {
    
            return o;
        }
        // Avoid being right actuator The monitoring interface of 
        if (((ServletServerHttpRequest) serverHttpRequest).getServletRequest().getRequestURI().contains(ADMIN_ACTUATOR)) {
    
            return o;
        }
        if (o instanceof Result) {
    
            return o;
        }
        return new Result<>(0, "",o);


    }
}

Key methods beforeBodyWrite, stay SpringMVC The main process has mentioned
 Insert picture description here
It will be called , Thus, the return value is encapsulated into a custom Result Inside , It is convenient for the front end to uniformly process the status code .

Two 、 Error reporting and resolution

2.1 Integrate swagger Report errors

In integration swagger after , Always report an error , Interface documents can't come out , The following error message appears :

Unable to render this definition
The provided definition does not specify a valid version field.
Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: “2.0” and those that match openapi: 3.0.n (for example, openapi: 3.0.0).

Baidu has been useless for a long time , Last check /v3/api-docs After the interface finds that the return value is encapsulated ,swagger The front end of cannot parse the custom structure , Result in an error .
Solution : Determine the specific interface path without encapsulation

2.2 Interface directly returns String Type error

Define an interface

@GetMapping("/getUserName")
    @ApiOperation(" according to ID Get users ")
    public String getUserName() {
    
        return "username";
    }

Use Swagger Send a request
 Insert picture description here
Wrong report , be supposed to Result Cannot be forced to String, There is no way but to find a solution from the source code , according to SpringMVC Main process , The key place is still here
 Insert picture description here
Spring Provides 10 A message converter ,String stay Json In front of . call beforeBodyWrite After method
 Insert picture description here
First match to the converter StringHttpMessageConverter, And will be body Encapsulated into Result type
 Insert picture description here
StringHttpMessageConverter The generics of are String, Get into write After method
 Insert picture description here
t The type is Result, A subclass of addDefaultHeaders What the method needs is String
 Insert picture description here
Cause type strong conversion exception
Solution : Interface return String Type , Use in advance Result encapsulation . Can't be in beforeBodyWrite Judgment is StringHttpMessageConverter No encapsulation , because StringHttpMessageConverter After the conversion, return to ,Json The converter will not be used .

@GetMapping("/getUserName")
    @ApiOperation(" according to ID Get users ")
    public Result<String> getUserName() {
    
        return new Result<>("username");
    }
原网站

版权声明
本文为[Integer_ Double]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207231626275179.html