Reputation: 42418
I am using springboot with jersey as restful API. Now I want to integrate swagger2 into my project but it doesn't work. When I run my application and access http://localhost:8080/swagger-ui.html. I got the swagger web page but no api is showing(see below image). It seems that swagger didn't find my api classes.
Below is the dependencies I added.
compile "io.springfox:springfox-swagger2:2.5.0"
compile 'io.springfox:springfox-swagger-ui:2.5.0'
Below is my application class:
@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;
}
}
Below is my jersey config class:
@Configuration
@ApplicationPath("/ticket")
public class JerseyConfig extends ResourceConfig {
public JerseyConfig(){
register(Helloworld.class);
}
}
Upvotes: 4
Views: 5598
Reputation: 3475
As previously answered in this thread, @EnableSwagger2
annotation and springfox
dependencies would work if the endpoints are implemented using Spring MVC instead of Jersey.
In order to document your Jersey-implemented endpoints:
1) Make sure your Spring Boot app scans for components located in specific packages (ie com.asimio.jerseyexample.config) via:
@SpringBootApplication(
scanBasePackages = {
"com.asimio.jerseyexample.config", "com.asimio.jerseyexample.rest"
}
)
2) Jersey configuration class implementation:
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);
}
}
3) Resource implementation using JAX-RS (Jersey) and Swagger annotations:
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");
}
...
}
4) Make sure your app's Spring Boot configuration file makes a distinction between Spring MVC (for actuator endpoints) and Jersey (for resources) endpoints:
application.yml
...
# 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
...
These steps are better detailed in a blog post I created some time ago, Microservices using Spring Boot, Jersey Swagger and Docker
It references source code in my Bitbucket repo and a running example
Upvotes: 1
Reputation: 5369
Jersey isn't supported, please refer to this answer. Given that the answer comes from SpringFox library's author, I'd say the info is solid.
Upvotes: 1