[英]How to add Swagger related static files to Spring Boot + Jersey app?
我试图将Swagger支持添加到我的REST API中,但是对于如何将Swagger相关的静态内容(HTML,JS)文件添加到Spring Boot应用程序中,我感到困惑。
我使用以下依赖项:
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内容。
但是我不知道如何将Swagger相关的静态HTML内容添加到我的应用程序。 我可以看到此内容位于springfox-swagger-ui.jar
,可以将其作为maven依赖项添加到我的项目中,但是如何从此jar中解压缩内容呢?
什么是在静态Swagger文件中用我的URL覆盖默认的swagger.json URL的正确方法,以便在打开swagger-ui.html
时Swagger立即显示我的REST API。
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>${swagger-ui.version}</version>
</dependency>
请不要包含springfox-swagger-ui.jar
,它是要与Spring
的RestController
。
您必须立即解决它,但它可能会对其他人有所帮助,因此这是完整的过程,因为我也在寻找教程。
我将Swagger V2
与Spring Boot 2
,这是一个简单的3步过程。
步骤1:在pom.xml
文件中添加所需的依赖项。 第二个依赖项是可选的,仅当需要Swagger UI
时才使用它。
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
步骤2:添加配置类
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public static final Contact DEFAULT_CONTACT = new Contact("Usama Amjad", "https://stackoverflow.com/users/4704510/usamaamjad", "hello@email.com");
public static final ApiInfo DEFAULT_API_INFO = new ApiInfo("Article API", "Article API documentation sample", "1.0", "urn:tos",
DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList<VendorExtension>());
@Bean
public Docket api() {
Set<String> producesAndConsumes = new HashSet<>();
producesAndConsumes.add("application/json");
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(DEFAULT_API_INFO)
.produces(producesAndConsumes)
.consumes(producesAndConsumes);
}
}
步骤3:安装完成,现在您需要在controllers
记录API
@ApiOperation(value = "Returns a list Articles for a given Author", response = Article.class, responseContainer = "List")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success"),
@ApiResponse(code = 404, message = "The resource you were trying to reach is not found") })
@GetMapping(path = "/articles/users/{userId}")
public List<Article> getArticlesByUser() {
// Do your code
}
用法:
Swagger UI:您可以通过http://localhost:8080/swagger-ui.html
访问它
邮递员:您还可以从http://localhost:8080/v2/api-docs
访问您的文档JSON
,只需将其复制粘贴到邮递员中即可使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.