Specifying dependency to Swagger

To use Swagger in your Jersey 2 application, specify the dependency to swagger-jersey2-jaxrs jar. If you use Maven for building the source, the dependency to the swagger-core library will look as follows:

<dependency> 
    <groupId>com.wordnik</groupId> 
    <artifactId>swagger-jersey2-jaxrs</artifactId> 
    <version>1.5.1-M1</version>  
    <!-use the appropriate version here,  
    1.5.x supports Swagger 2.0 spec --> 
</dependency> 
You should be careful while choosing the swagger-core version for your product. Note that swagger-core 1.3 produces the Swagger 1.2 definitions, whereas swagger-core 1.5 produces the Swagger 2.0 definitions.

The next step is to hook the Swagger provider components into your Jersey application. This is done by configuring the Jersey servlet (org.glassfish.jersey.servlet.ServletContainer) in web.xml, as shown here:

<servlet> 
    <servlet-name>jersey</servlet-name> 
    <servlet-class> 
        org.glassfish.jersey.servlet.ServletContainer 
    </servlet-class> 
    <init-param> 
        <param-name>jersey.config.server.provider.packages 
        </param-name> 
        <param-value> 
            com.wordnik.swagger.jaxrs.json, 
            com.packtpub.rest.ch7.swagger 
        </param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
    <servlet-mapping> 
        <servlet-name>jersey</servlet-name> 
        <url-pattern>/webresources/*</url-pattern> 
    </servlet-mapping> 

To enable the Swagger documentation features, it is necessary to load the Swagger framework provider classes from the com.wordnik.swagger.jaxrs.listing package. The package names of the JAX-RS resource classes and provider components are configured as the value for the jersey.config.server.provider.packages init parameter. The Jersey framework scans through the configured packages for identifying the resource classes and provider components during the deployment of the application. Map the Jersey servlet to a request URI so that it responds to the REST resource calls that match the URI.

If you prefer not to use web.xml, you can also use the custom application subclass for (programmatically) specifying all the configuration entries discussed here. To try this option, refer to https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-JAX-RS-Project-Setup.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.99.152