Coder Social home page Coder Social logo

spring-jersey-swagger-example's Introduction

Build Status

Spring Boot with Jersey and Swagger Example

This is an example on how to integrate Spring Boot, with Jersey, Swagger, and Swager UI.

Changes Needed

Swagger Configuration

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);
    }
}

Swagger UI Static Resources

  1. Download the Swagger UI static resources from here
  2. Copy the downloaded resources to /target/classes/static folder.
  3. Rename the index.html in /target/classes/static to swagger-ui.html.
  4. Replace the http://petstore.swagger.io/v2/swagger.json string in swagger-ui.html to swagger.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: filter

You 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>

Build

To build the JAR, execute the following command from the parent directory:

mvn clean install

Run

To run the application fromm command line,

java -jar target/spring-jersey-swagger-example-1.0.0.jar

Access Swagger Endpoints

Swagger UI

You can view the Swagger UI at http://localhost:8080/swagger-ui.html.

Swagger JSON

You can view Swagger JSON doc at http://localhost:8080/swagger.json

spring-jersey-swagger-example's People

Contributors

indrabasak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.