我已经按照以下链接使用Swagger with Spring为我的REST服务创建API文档。
http://jakubstas.com/spring-jersey-swagger-configuration/#comment-1726
一切顺利但当我尝试使用url http://localhost:8080/rest/api-docs访问api docs swagger时,我得到了无法读取swagger JSON。有人可以帮忙吗?
Swagger不在当地工作!您可以下载Swagger Ui for local
你试试这种方式。
@Configuration
@EnableSwagger
// Loads the spring beans required by the framework
public class MySwaggerConfig
{
private SpringSwaggerConfig springSwaggerConfig;
/**
* Required to autowire SpringSwaggerConfig
*/
@Autowired
public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig)
{
this.springSwaggerConfig = springSwaggerConfig;
}
/**
* Every SwaggerSpringMvcPlugin bean is picked up by the swagger-mvc
* framework - allowing for multiple swagger groups i.e. same code base
* multiple swagger resource listings.
*/
@Bean
public SwaggerSpringMvcPlugin customImplementation()
{
return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns(
".*?");
}
private ApiInfo apiInfo()
{
ApiInfo apiInfo = new ApiInfo(
"xx",
"xxxx",
"My Apps API terms of service",
"xxx",
null,
null);
return apiInfo;
}
}
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.5</version>
</dependency>
我通过向资源文件夹添加下一个文件解决了这个问题
swagger.properties
添加了财产:
springfox.documentation.swagger.v2.path = / API / swagger.json
然后在代码中添加:
@Configuration
@EnableSwagger2
@PropertySource(value = "classpath:swagger.properties")
public class PathConfiguration {
@Value("${springfox.documentation.swagger.v2.path}")
private String swagger2Endpoint;
然后用于Dockect配置的bean简单bean如:
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
它会使这个问题如下:
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("Swagger Group One API")
.select()
.apis(RequestHandlerSelectors.basePackage("com.xingyun"))
.paths(PathSelectors.any())
.build();
}
固定如下是好的
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.groupName("SwaggerGroupOneAPI")
.select()
.apis(RequestHandlerSelectors.basePackage("com.xingyun"))
.paths(PathSelectors.any())
.build();
}