无法让 Swagger UI 使用 Spring 启动
Unable to get Swagger UI working with Spring boot
我正在尝试让 Swagger UI 与 Spring Boot 1.2.1 一起工作。我按照 https://github.com/martypitt/swagger-springmvc 中的说明进行操作,并在我的 spring 配置中添加了 @EnableSwagger
。
我目前在去 http://localhost:8080/api-docs
时返回 JSON,但并不好 HTML。
我正在使用 Maven 并添加了对 swagger-ui 的依赖:
<dependency>
<groupId>org.ajar</groupId>
<artifactId>swagger-spring-mvc-ui</artifactId>
<version>0.4</version>
</dependency>
这是我的完整依赖项列表:
<dependencies>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.4</version>
</dependency>
<dependency>
<groupId>org.ajar</groupId>
<artifactId>swagger-spring-mvc-ui</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我也试过 http://localhost:8080/docs/index.html
作为 URL,但这只给出了 "Whitelabel Error Page"
更新:
我在 Github 上创建了一个测试项目来显示问题:https://github.com/wimdeblauwe/springboot-swagger-test
我有同样的问题,但这个 link 对我有用:http://localhost:8080/sdoc.jsp
它预先填充 swagger ui 资源 url 框:
http://localhost:8080${pageContext.request.contextPath}/api-文档
当我手动编辑它删除 ${pageContext.request.contextPath} 并点击探索时,它会显示我的 api 文档,我什至可以成功尝试我的端点。所以绝对是个问题,但可能不会选择 ${pageContext.request/contextPath}.
查看来源 javascript 有:
url: window.location.origin + "${pageContext.request.contextPath}/api-文档"
一动不动uihtml我把这一段编码为:
discoveryUrl:./resource-list.json"
希望对您有所帮助
我有 swagger-ui v0.4(使用 spring v4.14 和 swagger-springmvc v0.9.4)工作正常,虽然我有一些类似的问题第一的。看来这个 class 可以解决问题。
@Configuration
@EnableSwagger
public class SwaggerConfig extends WebMvcConfigurerAdapter {
@Autowired
private SpringSwaggerConfig springSwaggerConfig;
@Bean
public SwaggerSpringMvcPlugin customImplementation() {
return new SwaggerSpringMvcPlugin(springSwaggerConfig).apiInfo(
apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(/* strings */);
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
我相信相关的东西被覆盖了configureDefaultServletHandling
。在我的主 WebApplicationInitializer
上,我有:
@Import(SwaggerConfig.class)
最后,我解决了 UI 的位置框显示“http://localhost:8080${pageContext.request.contextPath}/api-docs”的问题,方法是将其包含在我的依赖项:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!--<version>8.0.15</version>-->
<scope>provided</scope>
</dependency>
提供与 JSP 处理相关的内容。它包含在 spring-boot
的依赖项中,但通常不包含在 provided
.
中
希望对您有所帮助。
您的问题出在您的 SwaggerConfiguration
文件中。您需要删除 @EnableWebMvc
,因为这会导致默认 Spring 引导视图解析器被默认 'SpringWebMvc' 覆盖,后者以不同方式提供静态内容。
默认情况下,Spring Boot 将从以下任何目录提供静态内容:
- /META-INF/resources/
- /资源/
- /静态/
- /public/
包括 webjars。
我遇到了同样的问题,我在文档中找到了这个:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-auto-configuration
If you want to take complete control of Spring MVC, you can add your own @Configuration
annotated with @EnableWebMvc
. If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own @Bean
of type WebMvcConfigurerAdapter
, but without @EnableWebMvc
.
希望对您有所帮助。
我建议您使用@EnableSwagger2 标签并按照此处的步骤和代码进行操作:https://github.com/sanketsw/SpringBoot_REST_API
我还使用了以下依赖项,它工作得很好:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
我已经为 Swagger 做了单独的配置,我的问题是没有 @EnableAutoConfiguration
它不能正常工作。
@Configuration
@EnableSwagger2
@EnableAutoConfiguration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
如果您在使用 springfox swagger-ui 3.x 或更高版本时遇到此问题..
试试下面的方法 url.. 它对我有用..
http://localhost:8080/swagger-ui/
有关完整的 swagger 文档步骤,请参阅:
http://muralitechblog.com/swagger-rest-api-dcoumentation-for-spring-boot/
正如上面 Tamas 所指出的,问题在于使用 @EnableWebMvc,它绕过了默认设置并跳过了 Swagger 需要的一些东西。对我来说,将其切换到 @EnableSwagger2 足以解决我的项目中的问题,该问题显示出类似的症状。
我正在尝试让 Swagger UI 与 Spring Boot 1.2.1 一起工作。我按照 https://github.com/martypitt/swagger-springmvc 中的说明进行操作,并在我的 spring 配置中添加了 @EnableSwagger
。
我目前在去 http://localhost:8080/api-docs
时返回 JSON,但并不好 HTML。
我正在使用 Maven 并添加了对 swagger-ui 的依赖:
<dependency>
<groupId>org.ajar</groupId>
<artifactId>swagger-spring-mvc-ui</artifactId>
<version>0.4</version>
</dependency>
这是我的完整依赖项列表:
<dependencies>
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.4</version>
</dependency>
<dependency>
<groupId>org.ajar</groupId>
<artifactId>swagger-spring-mvc-ui</artifactId>
<version>0.4</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我也试过 http://localhost:8080/docs/index.html
作为 URL,但这只给出了 "Whitelabel Error Page"
更新:
我在 Github 上创建了一个测试项目来显示问题:https://github.com/wimdeblauwe/springboot-swagger-test
我有同样的问题,但这个 link 对我有用:http://localhost:8080/sdoc.jsp
它预先填充 swagger ui 资源 url 框: http://localhost:8080${pageContext.request.contextPath}/api-文档
当我手动编辑它删除 ${pageContext.request.contextPath} 并点击探索时,它会显示我的 api 文档,我什至可以成功尝试我的端点。所以绝对是个问题,但可能不会选择 ${pageContext.request/contextPath}.
查看来源 javascript 有: url: window.location.origin + "${pageContext.request.contextPath}/api-文档"
一动不动uihtml我把这一段编码为:
discoveryUrl:./resource-list.json"
希望对您有所帮助
我有 swagger-ui v0.4(使用 spring v4.14 和 swagger-springmvc v0.9.4)工作正常,虽然我有一些类似的问题第一的。看来这个 class 可以解决问题。
@Configuration
@EnableSwagger
public class SwaggerConfig extends WebMvcConfigurerAdapter {
@Autowired
private SpringSwaggerConfig springSwaggerConfig;
@Bean
public SwaggerSpringMvcPlugin customImplementation() {
return new SwaggerSpringMvcPlugin(springSwaggerConfig).apiInfo(
apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfo(/* strings */);
}
@Override
public void configureDefaultServletHandling(
DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
我相信相关的东西被覆盖了configureDefaultServletHandling
。在我的主 WebApplicationInitializer
上,我有:
@Import(SwaggerConfig.class)
最后,我解决了 UI 的位置框显示“http://localhost:8080${pageContext.request.contextPath}/api-docs”的问题,方法是将其包含在我的依赖项:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<!--<version>8.0.15</version>-->
<scope>provided</scope>
</dependency>
提供与 JSP 处理相关的内容。它包含在 spring-boot
的依赖项中,但通常不包含在 provided
.
希望对您有所帮助。
您的问题出在您的 SwaggerConfiguration
文件中。您需要删除 @EnableWebMvc
,因为这会导致默认 Spring 引导视图解析器被默认 'SpringWebMvc' 覆盖,后者以不同方式提供静态内容。
默认情况下,Spring Boot 将从以下任何目录提供静态内容:
- /META-INF/resources/
- /资源/
- /静态/
- /public/
包括 webjars。
我遇到了同样的问题,我在文档中找到了这个:http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-auto-configuration
If you want to take complete control of Spring MVC, you can add your own
@Configuration
annotated with@EnableWebMvc
. If you want to keep Spring Boot MVC features, and you just want to add additional MVC configuration (interceptors, formatters, view controllers etc.) you can add your own@Bean
of typeWebMvcConfigurerAdapter
, but without@EnableWebMvc
.
希望对您有所帮助。
我建议您使用@EnableSwagger2 标签并按照此处的步骤和代码进行操作:https://github.com/sanketsw/SpringBoot_REST_API
我还使用了以下依赖项,它工作得很好:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
<scope>compile</scope>
</dependency>
我已经为 Swagger 做了单独的配置,我的问题是没有 @EnableAutoConfiguration
它不能正常工作。
@Configuration
@EnableSwagger2
@EnableAutoConfiguration
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
如果您在使用 springfox swagger-ui 3.x 或更高版本时遇到此问题..
试试下面的方法 url.. 它对我有用..
http://localhost:8080/swagger-ui/
有关完整的 swagger 文档步骤,请参阅: http://muralitechblog.com/swagger-rest-api-dcoumentation-for-spring-boot/
正如上面 Tamas 所指出的,问题在于使用 @EnableWebMvc,它绕过了默认设置并跳过了 Swagger 需要的一些东西。对我来说,将其切换到 @EnableSwagger2 足以解决我的项目中的问题,该问题显示出类似的症状。