使用Spring MVC(而不是Spring Boot)配置Swagger2的正确方法是什么。
当前我添加了以下组件
Maven Dependency
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!--Dependency for swagger ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
在调度程序servlet上下文中
<bean id="swagger2Config" class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration"/>
<mvc:resources location="/resources/" mapping="/resources/**"
order="1" />
<mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html" />
<mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**" />
<mvc:default-servlet-handler />
并添加了配置
@EnableSwagger2
public class SwaggerConfiguration extends WebMvcConfigurerAdapter{
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
当我尝试启动应用程序时,我的应用程序上下文就是示例
http://localhost:8080/sample/swagger-ui.html,
我遇到错误
Unable to infer base url. This is common when using dynamic servlet
registration or when the API is behind an API Gateway. The base url is the
root of where all the swagger resources are served. For e.g. if the api is
available at http://example.org/api/v2/api-docs then the base url is
http://example.org/api/. Please enter the location manually:
如果我尝试过 http://localhost:8080/sample/v2/api-docs/
我要例外了
Handler processing failed; nested exception is java.lang.NoSuchMethodError
on org.springframework.web.util.UriComponentsBuilder.fromHttpRequest
我在这里想念其他东西吗?
顺便说一句,我正在使用Spring 3.2.9和Servlet API 2.5
谢谢大家
答案 0 :(得分:0)
似乎在启动过程中未扫描您的@ EnableSwagger2批注。尝试在调度程序servlet xml文件中使用组件扫描。像下面这样
<context:component-scan base-package="your.package.name" />
传递存在SwaggerConfiguration类的包。那应该可以解决问题。
答案 1 :(得分:0)
实际上基于stackoverflow.com/questions/33518672/…和github.com/springfox/springfox/blob/v1.0.2/README.md,Spring 3.x与Swagger 2不兼容。所以我最终使用了Swagger 1.我正在遵循github.com/martypitt/swagger-springmvc-example中给出的示例。成功集成Swagger 1后,我将进行更新