Unable to infer base url… springfox-swagger2 version 2.9.2

前端 未结 2 439
野性不改
野性不改 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<Object> {
        
        @Override
        public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
            return true;
        }
    
        @Override
        public Object beforeBodyWrite(
                Object body,
                MethodParameter methodParameter,
                MediaType mediaType,
                Class<? extends HttpMessageConverter<?>> 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<String, Object> 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;
        }
    }
    
    0 讨论(0)
  • 2021-01-16 16:01

    Now, I managed to solve my issue. One of the issues was the pathMApping.

    I needed to change my configuration to explicitly to "/", like here

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfiguration {
      @Bean
      public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any())
            .build()
            .pathMapping("/");
      }
    }
    

    Other issue was I used ResponseBodyAdvice

    import io.falcon.automate.api.web.views.BaseView;
    import io.falcon.automate.api.web.views.ErrorView;
    import org.springframework.core.MethodParameter;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.server.ServerHttpRequest;
    import org.springframework.http.server.ServerHttpResponse;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
    
    
    @ControllerAdvice
    public class ResponseAdvice implements ResponseBodyAdvice<Object> {
    
        @Override
        public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
            return true;
        }
    
        /**
         * Wraps all responses in base view class
         * @param body
         * @param returnType
         * @param selectedContentType
         * @param selectedConverterType
         * @param request
         * @param response
         * @return
         */
        @Override
        public Object beforeBodyWrite(
                Object body,
                MethodParameter returnType,
                MediaType selectedContentType,
                Class<? extends HttpMessageConverter<?>> selectedConverterType,
                ServerHttpRequest request,
                ServerHttpResponse response
        ) {
            if (body instanceof ErrorView) {
                return body;
            }
            return new BaseView(body);
        }
    }
    
    

    If I commented out my response wrapper class, everything works like a charm.....

    The only issue, that I need my response wrapper. :/

    0 讨论(0)
提交回复
热议问题