Unable to infer base url… springfox-swagger2 version 2.9.2

a 夏天 提交于 2019-12-01 12:34:24

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. :/

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) {          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;     } } 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!