san*_*ndy 6 swagger swagger-ui spring-boot
我正在尝试将 Swagger UI 用于使用 Spring Boot 框架开发的 API 文档。
1)pom中的依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${springfox-version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-core</artifactId>
<version>${swagger-core-version}</version>
</dependency>
<properties>
<springfox-version>2.5.0</springfox-version>
<swagger-core-version>1.5.10</swagger-core-version>
</properties>
Run Code Online (Sandbox Code Playgroud)
2) Docket 配置
@ComponentScan(basePackages = {"com.testApp.*"})
@Configuration
@EnableSwagger2
public class Application {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select().apis(
RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
Run Code Online (Sandbox Code Playgroud)
3)com.testApp包中的资源配置
@Path("/resources")
@Api(value = "Test resource", produces = "application/json")
public class MyResource {
@Autowired
public SomeClass someclass;
/**
*
* @param uriInfo
* @return
* @throws PlatformException
*/
@ApiOperation(value = "Gets a hello resource. World Version 1 (version in Accept Header)", response = String.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Hello resource found"),
@ApiResponse(code = 404, message = "Hello resource not found")
})
@GET
@Produces({ MediaType.APPLICATION_JSON })
public String loadResouces(@Context UriInfo uriInfo) {
//method defination
}
Run Code Online (Sandbox Code Playgroud)
该服务在端口 9001 上运行。无论何时点击 localhost:8080/swagger-ui.html。它返回一个 swagger-ui 的空页面。我尝试了 Docket 的一些属性,如主机、路径映射等。但我无法使用它生成文档。
据我所知,仅springfox-swagger2支持使用 实现的 API Spring MVC。
如果您更喜欢使用端点来实现端点,JAX-RS但仍然使用Swagger它们来记录端点,请查看此答案。
可以在我之前创建的博客Microservices using Spring Boot, Jersey Swagger and Docker中找到“操作方法”
| 归档时间: |
|
| 查看次数: |
7476 次 |
| 最近记录: |