spring boot application configuration for JaxRs based Swagger

suman j picture suman j · Mar 27, 2014 · Viewed 7.1k times · Source

Is there an example available to configure spring boot+Jersey+JaxRs application for Swagger?

Referring to this post, I have below code in my app and am not able to bring up the swagger UI and neither able to list the resources using

http://localhost:8080/api/root/{funcId}/entities

.

ApplicationInitializer class: (Groovy)


@Configuration
@EnableAutoConfiguration
class Application extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run( Application.class, args)
    }
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources( Application, "classpath:/META-INF/com/company/automation/functionality/bootstrap.xml");
    }

    @Bean
    ServletRegistrationBean jerseyServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new SpringServlet());
        Map params = ["com.sun.jersey.config.property.packages":"com.company.automation.functionality.impl;com.wordnik.swagger.jersey.listing"]
        registration.setInitParameters(params)
        return registration;
    }

    @Bean
    ServletRegistrationBean jerseyJaxrsConfig() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new JerseyJaxrsConfig());
        Map params = ["swagger.api.basepath":"http://localhost:8080/api", "api.version":"1.0"]
        registration.setInitParameters(params)
        return registration;
    }
}

Resource:


@Component
@Path('root/{funcId}/entities')
@Api (value = "root/{funcId}/entities", description = "Operations about entity Details")
@CompileStatic
class EntityDetailsResource {
..
}

[EDIT]. I am using Jersey 1.16 and can not use Jersey 2 due to application impact

Answer

Dave Syer picture Dave Syer · Mar 28, 2014

I was able to get it working, but I'm not sure how good the Spring support is in Jersey 1.x, so I did it with 2.7 (that link you posted is for JAX-RS 1.x). This worked:

@Configuration
@ComponentScan
@EnableAutoConfiguration
@Path("/")
@Api(value = "home", description = "Demo API")
public class Application extends ResourceConfig implements CommandLineRunner {

    @GET
    @ApiOperation(value = "Get Greeting", notes = "Returns greeting")
    public String home() {
        return "Hello";
    }

    @Override
    public void run(String... args) throws Exception {
        SwaggerConfig config = ConfigFactory.config();
        config.setBasePath("http://localhost:8080/");
        config.setApiVersion("1.0.0");
        ScannerFactory.setScanner(new DefaultJaxrsScanner());
        ClassReaders.setReader(new JerseyApiReader());
    }

    public Application() {
        register(Application.class);
        packages("com.wordnik.swagger.jersey.listing");
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

It uses spring-boot-jersey, which is experimental, to create the Jersey servlet, but you can do that easily with a ServletRegistration @Bean if you want to stick to vanilla Boot.

I had to pin the Jersey versions and add some exclusions. Here are my dedendencies:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
    </dependency>
    <dependency>
        <groupId>com.wordnik</groupId>
        <artifactId>swagger-jersey2-jaxrs_2.10</artifactId>
        <version>1.3.2</version>
        <exclusions>
            <exclusion>
                <artifactId>jsr311-api</artifactId>
                <groupId>javax.ws.rs</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.core</groupId>
        <artifactId>jersey-server</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet-core</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.jersey.containers</groupId>
        <artifactId>jersey-container-servlet</artifactId>
        <version>2.7</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-jersey</artifactId>
        <version>1.0.0.BUILD-SNAPSHOT</version>
    </dependency>
</dependencies>