SpringFox not finding jax-rs endpoints
By default SpringFox will document your REST services implemented using Spring MVC - it is as simple as adding @EnableSwagger2
annotation
@SpringBootApplication
@EnableSwagger2
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
I managed to get SpringFox working with JAX-RS At first I have added some swagger dependencies along with SpringFox:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-jersey")
compile("io.springfox:springfox-swagger-ui:2.4.0")
compile("io.springfox:springfox-swagger2:2.4.0")
compile("io.swagger:swagger-jersey2-jaxrs:1.5.8")
testCompile("junit:junit")
}
Then I have enabled JAX-RS (Jersey) with Swagger in my spring-boot application:
@Component
@ApplicationPath("/api")
public static class JerseyConfig extends ResourceConfig {
public JerseyConfig() {
BeanConfig swaggerConfig = new BeanConfig();
swaggerConfig.setBasePath("/api");
SwaggerConfigLocator.getInstance().putConfig(SwaggerContextService.CONFIG_ID_DEFAULT, swaggerConfig);
packages(getClass().getPackage().getName(), ApiListingResource.class.getPackage().getName());
}
}
Please note that all JAX-RS endpoints will be under /api
context - otherwise it would conflict with Spring-MVC dispatcher
Finally we should add the swagger json generated for Jersey endpoints to Springfox:
@Component
@Primary
public static class CombinedSwaggerResourcesProvider implements SwaggerResourcesProvider {
@Resource
private InMemorySwaggerResourcesProvider inMemorySwaggerResourcesProvider;
@Override
public List<SwaggerResource> get() {
SwaggerResource jerseySwaggerResource = new SwaggerResource();
jerseySwaggerResource.setLocation("/api/swagger.json");
jerseySwaggerResource.setSwaggerVersion("2.0");
jerseySwaggerResource.setName("Jersey");
return Stream.concat(Stream.of(jerseySwaggerResource), inMemorySwaggerResourcesProvider.get().stream()).collect(Collectors.toList());
}
}
That's it! Now your JAX-RS endpoints will be documented by Swagger. I used following sample endpoint:
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Component;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Component
@Path("/hello")
@Api
public class Endpoint {
@GET
@ApiOperation("Get message")
@Produces(MediaType.TEXT_PLAIN)
public String message() {
return "Hello";
}
}
Now when you start your server and go to http://localhost:8080/swagger-ui.html you will see the documentation for JAX-RS endpoints. Use combobox on top of the page to switch to the documentation of Spring MVC endpoints