This is an example on how to integrate Spring Boot, with Jersey, Swagger, and Swager UI.
You need the following swagger dependency:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jersey2-jaxrs</artifactId>
<version>${swagger.jersey2.version}</version>
</dependency>Heare are the Swagger configuration changes required to generate the
swagger.json by the io.swagger:swagger-jersey2-jaxrs library:
@Configuration
public class JerseyConfiguration extends ResourceConfig {
@Autowired
public JerseyConfiguration() throws UnknownHostException {
register(BookController.class);
configureSwagger();
property(ServletProperties.FILTER_FORWARD_ON_404, true);
}
public void configureSwagger() {
this.register(ApiListingResource.class);
this.register(SwaggerSerializers.class);
BeanConfig config = new BeanConfig();
config.setConfigId("spring-jersey-swagger-example");
config.setTitle("Spring, Jersey, and Swagger Example");
config.setVersion("1.0.0");
config.setBasePath("/");
config.setResourcePackage("com.basaki");
config.setScan(true);
}
}- Download the Swagger UI static resources from here
- Copy the downloaded resources to
/target/classes/staticfolder. - Rename the
index.htmlin/target/classes/statictoswagger-ui.html. - Replace the
http://petstore.swagger.io/v2/swagger.jsonstring inswagger-ui.htmltoswagger.json.
In this example, all these changes are executed dynamically during the
build process by the plugins specified in pom.xml.
By default, Spring Boot configures the Jersey servlet container as a servlet.
By default Jersey will be set up as a Servlet in a @Bean of type ServletRegistrationBean named jerseyServletRegistration. You can disable or override that bean by creating one of your own with the same name. You can also use a Filter instead of a Servlet by setting spring.jersey.type=filter (in which case the @Bean to replace or override is jerseyFilterRegistration).
It needs to be changed into a filter in order to pickup the Swagger static
resources. It can be achieved by modifying the application.ymlfile:
spring:
jersey:
type: filterYou also need the following dependency to view swagger-ui.html,
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>To build the JAR, execute the following command from the parent directory:
mvn clean install
To run the application fromm command line,
java -jar target/spring-jersey-swagger-example-1.0.0.jar
You can view the Swagger UI at http://localhost:8080/swagger-ui.html.
You can view Swagger JSON doc at http://localhost:8080/swagger.json
