Unable to infer base url… springfox-swagger2 version 2.9.2

前端 未结 2 455
野性不改
野性不改 2021-01-16 15:11

Version of springfox-swagger that I am using

What kind of issue is this? Project structure: SwaggerConfig is in automate.api.config.swagger.Swagger

2条回答
  •  耶瑟儿~
    2021-01-16 15:51

    To solve the issue you just need to add the following to the ResponseBodyAdvice implemented class, at @ControllerAdvice annotation you just need to define the base package of your application directory, so this won't return a null when calling the swagger-ui url.

    For example, have a look at first line @ControllerAdvice annotation input parameter

    @ControllerAdvice("com.main.abc.package")
    public class AdapterAdvice implements ResponseBodyAdvice {
        
        @Override
        public boolean supports(MethodParameter methodParameter, Class> aClass) {
            return true;
        }
    
        @Override
        public Object beforeBodyWrite(
                Object body,
                MethodParameter methodParameter,
                MediaType mediaType,
                Class> aClass,
                ServerHttpRequest serverHttpRequest,
                ServerHttpResponse serverHttpResponse) {
            // just some wrapper logic, you can just omit the below and return the body Object you got as a param
            Map data = new HashMap<>();
            data.put("serverTime", new Date(System.currentTimeMillis()));
            if(body instanceof Map && ((Map)body).get("error") != null ){
                data.put("isSuccess", false);
                if(((Map)body).get("trace") != null){
                    ((Map)body).remove("trace");
                }
            } else if (body instanceof ApiError && ((ApiError)body).getResponseStatusCode().equalsIgnoreCase("0")){
                data.put("isSuccess", false);
            } else {
                data.put("isSuccess", true);
            }
            data.put("mainResponse",body);
    
            return data;
        }
    }
    
        

    提交回复
    热议问题