Search code examples
javarestweb-servicesspring-bootjersey

Configuring Jersey with Spring boot


I am trying to configure the spring boot with jersey but it seems jersey annotations are not working with spring boot. can you please help me out.

I have tried @RestController instead of @Component and @RequestMapping instead of @Path in service class.

pom.xml

<project>
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.hotel</groupId>
    <artifactId>reservations</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>reservations</name>
    <description>Demo project for Spring Boot</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.8.RELEASE</version>
        <relativePath/>
        <!--  lookup parent from repository  -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
                <version>1.4.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jersey</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Spring Boot Application Xml

package org.hotel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ReservationApplication {

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

}

service class with jersey annotations

package org.hotel.webservices;
import org.springframework.stereotype.Component;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Component
@Path("/rooms")
public class AddRoomService {

    @GET
    public String addRoomService(){
        return "success";
    }
}

Solution

  • Nice tutorial online about this: Spring Boot Jersey Example July 14, 2017 by Lokesh Gupta. This seems to be the part you're missing.

    Jersey Configuration

    1: Now we have a JAX-RS resource and we want to access it from spring boot application which include Jersey dependency. Let’s register this resource as Jersey resource.

    package com.howtodoinjava.jerseydemo;
    
    import org.glassfish.jersey.server.ResourceConfig;
    import org.springframework.stereotype.Component;
    
    @Component
    public class JerseyConfig extends ResourceConfig 
    {
        public JerseyConfig() 
        {
            register(UserResource.class);
        }
    }
    

    Look at the @Component annotation. It enables this class to be registered while spring boot auto scans the java classes in source folder.

    2: ResourceConfig provides advanced capabilities to simplify registration of JAX-RS components. 3: Extend spring boot application with SpringBootServletInitializer.

    package com.howtodoinjava.jerseydemo;
    
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.builder.SpringApplicationBuilder;
    import org.springframework.boot.web.support.SpringBootServletInitializer;
    
    @SpringBootApplication
    public class JerseydemoApplication extends SpringBootServletInitializer 
    {
        public static void main(String[] args) 
        {
            new JerseydemoApplication().configure(new SpringApplicationBuilder    (JerseydemoApplication.class)).run(args);
        }
    }