问题描述
我正在尝试向我的REST API添加Swagger支持,但是对于如何将Swagger相关的静态内容(HTML,JS)文件添加到Spring Boot应用程序中,我感到困惑.
I am trying to add Swagger support to my REST API but I am confused how to add Swagger related static content (HTML, JS) files to my Spring Boot application.
我使用以下依赖项:
-
spring-boot-starter-parent:2.0.1.RELEASE
-
spring-boot-starter-jersey:2.0.1.RELEASE
-
swagger-jersey2-jaxrs:1.5.18
spring-boot-starter-parent:2.0.1.RELEASE
spring-boot-starter-jersey:2.0.1.RELEASE
swagger-jersey2-jaxrs:1.5.18
这是我的招摇配置:
@Configuration
public class SwaggerConfig {
@Bean
public BeanConfig swaggerConfiguration() {
final BeanConfig beanConfig = new BeanConfig();
beanConfig.setResourcePackage("a.b.c");
beanConfig.setScan(true);
beanConfig.setPrettyPrint(true);
return beanConfig;
}
}
以及球衣配置:
@Component
public class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
register(ImageResource.class);
register(io.swagger.jaxrs.listing.ApiListingResource.class);
register(io.swagger.jaxrs.listing.SwaggerSerializers.class);
}
}
当我打开 http://localhost:8090/swagger.json 然后我可以看到预期的Swagger JSON内容.
This part works like a charm, when I open http://localhost:8090/swagger.json then I can see the expected Swagger JSON content.
但是我不知道如何将Swagger相关的静态HTML内容添加到我的应用程序中.我可以看到此内容位于springfox-swagger-ui.jar
中,并且可以将其作为maven依赖项添加到我的项目中,但是如何从该jar中解压缩内容?
But I do not know, how to add the Swagger related static HTML content to my application. I can see that this content is in the springfox-swagger-ui.jar
and I can add it to my project as a maven dependency, but how I can unpack the content from this jar?
用静态Swagger文件中的URL覆盖默认的swagger.json URL的正确方法是什么,以便当我打开swagger-ui.html
时Swagger立即显示我的REST API.
And what is the proper way to overwrite the default swagger.json URL with my URL in the static Swagger file in order to Swagger show my REST API immediately when I open swagger-ui.html
.
推荐答案
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>${swagger-ui.version}</version>
</dependency>
请不要包含springfox-swagger-ui.jar
,这是为了与Spring
的RestController
一起使用.
Please, do not include springfox-swagger-ui.jar
, it's meant to work with Spring
's RestController
.
这篇关于如何将Swagger相关的静态文件添加到Spring Boot + Jersey应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!