我使用springboot和jersey作为restful API。现在我想将swagger2集成到我的项目中,但它不起作用。当我运行我的应用程序并访问http://localhost:8080/swagger-ui.html时。我得到了一个昂首阔步的网页,但没有显示api(见下图)。似乎招摇没有找到我的api课程。
以下是我添加的依赖项。
compile "io.springfox:springfox-swagger2:2.5.0"
compile 'io.springfox:springfox-swagger-ui:2.5.0'
以下是我的申请类:
@SpringBootApplication
@EnableSwagger2
@EnableAutoConfiguration
@Configuration
@ComponentScan(value = "com.ticket.api")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Bean
public Docket documentation() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.lenovo.ticket.api"))
.paths(PathSelectors.any())
.build().pathMapping("/")
.useDefaultResponseMessages(false);
}
@Bean
UiConfiguration uiConfig() {
return UiConfiguration.DEFAULT;
}
}
以下是我的球衣配置类:
@Configuration
@ApplicationPath("/ticket")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig(){
register(Helloworld.class);
}
}
答案 0 :(得分:1)
不支持泽西岛,请参阅this answer。鉴于答案来自SpringFox图书馆的作者,我会说信息是可靠的。
答案 1 :(得分:1)
如前所述,如果端点是使用Spring MVC而不是Jersey实现的,那么springfox
注释和@SpringBootApplication(
scanBasePackages = {
"com.asimio.jerseyexample.config", "com.asimio.jerseyexample.rest"
}
)
依赖将会起作用。
为了记录您的Jersey实施的端点:
1) 确保您的Spring Boot应用程序通过以下方式扫描位于特定软件包中的组件(即com.asimio.jerseyexample.config):
package com.asimio.jerseyexample.config;
...
@Component
public class JerseyConfig extends ResourceConfig {
@Value("${spring.jersey.application-path:/}")
private String apiPath;
public JerseyConfig() {
// Register endpoints, providers, ...
this.registerEndpoints();
}
@PostConstruct
public void init() {
// Register components where DI is needed
this.configureSwagger();
}
private void registerEndpoints() {
this.register(HelloResource.class);
// Access through /<Jersey's servlet path>/application.wadl
this.register(WadlResource.class);
}
private void configureSwagger() {
// Available at localhost:port/swagger.json
this.register(ApiListingResource.class);
this.register(SwaggerSerializers.class);
BeanConfig config = new BeanConfig();
config.setConfigId("springboot-jersey-swagger-docker-example");
config.setTitle("Spring Boot + Jersey + Swagger + Docker Example");
config.setVersion("v1");
config.setContact("Orlando L Otero");
config.setSchemes(new String[] { "http", "https" });
config.setBasePath(this.apiPath);
config.setResourcePackage("com.asimio.jerseyexample.rest.v1");
config.setPrettyPrint(true);
config.setScan(true);
}
}
2)Jersey配置类实现:
package com.asimio.jerseyexample.rest.v1;
...
@Component
@Path("/")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "Hello resource", produces = "application/json")
public class HelloResource {
private static final Logger LOGGER = LoggerFactory.getLogger(HelloResource.class);
@GET
@Path("v1/hello/{name}")
@ApiOperation(value = "Gets a hello resource. Version 1 - (version in URL)", response = Hello.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Hello resource found"),
@ApiResponse(code = 404, message = "Hello resource not found")
})
public Response getHelloVersionInUrl(@ApiParam @PathParam("name") String name) {
LOGGER.info("getHelloVersionInUrl() v1");
return this.getHello(name, "Version 1 - passed in URL");
}
...
}
3)使用JAX-RS(Jersey)和Swagger注释的资源实现:
...
# Spring MVC dispatcher servlet path. Needs to be different than Jersey's to enable/disable Actuator endpoints access (/info, /health, ...)
server.servlet-path: /
# Jersey dispatcher servlet
spring.jersey.application-path: /api
...
4)确保您的应用程序的Spring Boot配置文件区分Spring MVC(用于执行器端点)和Jersey(用于资源)端点:
<强> application.yml 强>
paste
这些步骤在我之前创建的博文QSpacerItem
它引用了我的Bitbucket仓库中的源代码和正在运行的示例