这是什么问题?
项目结构:
SwaggerConfig 位于automate.api.config.swagger.SwaggerConfiguration
中
Auth Config 位于automate.api.web.auth.keycloak.FalconKeycloakConfigurerAdapter
依赖项
关于Spring Boot
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
大张旗鼓
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${spring.springfox-swagger2.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${spring.springfox-swagger2.version}</version>
</dependency>
Swagger 2配置
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
安全配置
@Override
protected void configureAccessRules(final HttpSecurity http) throws Exception {
http.csrf()
.disable()
.authorizeRequests()
.antMatchers("/actuator/health")
.permitAll()
.antMatchers(HttpMethod.GET,
"/v2/api-docs",
"/swagger-resources/**",
"/swagger-ui.html**",
"/webjars/**",
"favicon.ico")
.permitAll();
}
我的一位控制器的示例
我所有的控制器都映射为/api/...
@RestController
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class ExampleResource {
private ExampleFacade catalog;
@Autowired
public ExampleResource(ExampleFacade catalog) {
this.catalog = catalog;
}
@RequestMapping(method = RequestMethod.GET, value = "/examples")
public List<ExampleDTO> index() {
return catalog.listExamples();
}
}
问题
/v2/api-docs
可以正常工作,我可以获取所有端点及其详细信息
/swagger-ui.html
带给我一个弹出窗口,显示“无法推断基本网址……”
然后,我在Spring-boot应用程序中打开了TRACE日志级别,例如
logging:
level:
org:
springframework:
web: TRACE
我的日志如下:
[2019-05-22 21:12:06] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet GET "/v2/api-docs", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:06] TRACE [nio-8080-exec-1] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: [null, SecurityContextHolderAwareRequestWrapper[ org.springframework.security.web.header.HeaderWriterFilter$HeaderWriterRequest@b9ed904]]
[2019-05-22 21:12:06] DEBUG [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json;q=0.8', given [text/html, application/xhtml+xml, image/webp, image/apng, application/signed-exchange;v=b3, application/xml;q=0.9, */*;q=0.8] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-22 21:12:06] TRACE [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data=springfox.documentation.spring.web.json.Json@65455d18)]
[2019-05-22 21:12:06] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:06] DEBUG [nio-8080-exec-1] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet GET "/swagger-ui.html", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-2] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]] and 5 interceptors
[2019-05-22 21:12:10] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-2] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-3] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/springfox.css?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-4] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/swagger-ui.css?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-3] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-3] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-22 21:12:10] TRACE [nio-8080-exec-4] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-4] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-22 21:12:10] TRACE [nio-8080-exec-6] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/swagger-ui-bundle.js?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-3] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-3] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-5] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/swagger-ui-standalone-preset.js?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-7] o.s.w.s.DispatcherServlet GET "/webjars/springfox-swagger-ui/springfox.js?v=2.9.2", parameters={masked}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-6] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-6] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-22 21:12:10] TRACE [nio-8080-exec-5] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-5] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-22 21:12:10] TRACE [nio-8080-exec-7] o.s.w.s.h.SimpleUrlHandlerMapping Matching patterns [/webjars/**, /**]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-7] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/webjars/"]] and 5 interceptors
[2019-05-22 21:12:10] TRACE [nio-8080-exec-4] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-4] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-7] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-7] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-5] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-5] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-6] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-6] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-8] o.s.w.s.DispatcherServlet GET "/swagger-resources/configuration/ui", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-8] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
[2019-05-22 21:12:10] TRACE [nio-8080-exec-8] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: []
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-8] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-8] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data=springfox.documentation.swagger.web.UiConfiguration@7594a0c7)]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-8] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-8] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [nio-8080-exec-9] o.s.w.s.DispatcherServlet GET "/swagger-resources/configuration/security", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [nio-8080-exec-9] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
[2019-05-22 21:12:10] TRACE [nio-8080-exec-9] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: []
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-9] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-9] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data=springfox.documentation.swagger.web.SecurityConfiguration@4605258b)]
[2019-05-22 21:12:10] TRACE [nio-8080-exec-9] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [nio-8080-exec-9] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:10] TRACE [io-8080-exec-10] o.s.w.s.DispatcherServlet GET "/swagger-resources", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:10] TRACE [io-8080-exec-10] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()
[2019-05-22 21:12:10] TRACE [io-8080-exec-10] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: []
[2019-05-22 21:12:10] DEBUG [io-8080-exec-10] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-22 21:12:10] TRACE [io-8080-exec-10] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data=[springfox.documentation.swagger.web.SwaggerResource@b35c0d2])]
[2019-05-22 21:12:10] TRACE [io-8080-exec-10] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:10] DEBUG [io-8080-exec-10] o.s.w.s.DispatcherServlet Completed 200 OK, headers={masked}
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet GET "/null/swagger-resources/configuration/ui", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]] and 5 interceptors
[2019-05-22 21:12:11] DEBUG [nio-8080-exec-1] o.s.w.s.r.ResourceHttpRequestHandler Resource not found
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:11] DEBUG [nio-8080-exec-1] o.s.w.s.DispatcherServlet Completed 404 NOT_FOUND, headers={masked}
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet "ERROR" dispatch for GET "/error", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: [SecurityContextHolderAwareRequestWrapper[ FirewalledRequest[ org.apache.catalina.core.ApplicationHttpRequest@dc7df80]]]
[2019-05-22 21:12:11] DEBUG [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data={timestamp=Wed May 22 21:12:11 CEST 2019, status=404, error=Not Found, message=No message available, path=/null/swagger-resources/configuration/ui})]
[2019-05-22 21:12:11] TRACE [nio-8080-exec-1] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:11] DEBUG [nio-8080-exec-1] o.s.w.s.DispatcherServlet Exiting from "ERROR" dispatch, status 404, headers={masked}
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet GET "/null/swagger-resources/configuration/security", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.h.SimpleUrlHandlerMapping Mapped to HandlerExecutionChain with [ResourceHttpRequestHandler ["classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/", "/"]] and 5 interceptors
[2019-05-22 21:12:12] DEBUG [nio-8080-exec-2] o.s.w.s.r.ResourceHttpRequestHandler Resource not found
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:12] DEBUG [nio-8080-exec-2] o.s.w.s.DispatcherServlet Completed 404 NOT_FOUND, headers={masked}
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet "ERROR" dispatch for GET "/error", parameters={}, headers={masked} in DispatcherServlet 'dispatcherServlet'
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.m.m.a.RequestMappingHandlerMapping Mapped to public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.m.m.a.ServletInvocableHandlerMethod Arguments: [SecurityContextHolderAwareRequestWrapper[ FirewalledRequest[ org.apache.catalina.core.ApplicationHttpRequest@723fa0fe]]]
[2019-05-22 21:12:12] DEBUG [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor Using 'application/json', given [application/json] and supported [application/json, application/*+json, application/json, application/*+json]
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.m.m.a.HttpEntityMethodProcessor Writing [BaseView(data={timestamp=Wed May 22 21:12:12 CEST 2019, status=404, error=Not Found, message=No message available, path=/null/swagger-resources/configuration/security})]
[2019-05-22 21:12:12] TRACE [nio-8080-exec-2] o.s.w.s.DispatcherServlet No view rendering, null ModelAndView returned.
[2019-05-22 21:12:12] DEBUG [nio-8080-exec-2] o.s.w.s.DispatcherServlet Exiting from "ERROR" dispatch, status 404, headers={masked}
不幸的是,我发现和的日志中的路径映射有些奇怪: GET“ / null / swagger-resources / configuration / ui” GET“ / null / swagger-resources / configuration / security”等...
请帮帮我! :)
我为此问题仔细阅读了以下内容:
Why does springfox-swagger2 UI tell me "Unable to infer base url."
https://github.com/springfox/springfox/issues/2191
https://github.com/springfox/springfox/issues/2907
swagger-ui.html 400 bad request
https://github.com/springfox/springfox/issues/2501 https://github.com/springfox/springfox/issues/2396
答案 0 :(得分:0)
现在,我设法解决了我的问题。 问题之一是pathMApping。
我需要将配置显式更改为“ /”,例如此处
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("/");
}
}
我曾经使用过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);
}
}
如果我注释掉响应包装器类,则所有内容都像一个魅力.....
唯一的问题是,我需要我的响应包装器。 :/
答案 1 :(得分:0)
要解决此问题,您只需将以下内容添加到ResponseBodyAdvice实现的类中,在@ControllerAdvice批注中,您只需定义应用程序目录的基本包即可,因此在调用swagger-时不会返回null。 ui网址。
例如,看看第一行@ControllerAdvice注释输入参数
join_more