Building Jersey Restful Web Services in Spring Boot.

What you need?
Following are needed:
  • java 8
  • maven 3.2
  • spring boot 1.5.8.RELEASE
Maven pom.xml

We will add spring-boot-starter-parent as a parent of our maven-based project. The added benefit of this is version management for Spring dependencies.

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.0.RELEASE</version>
</parent>

Adding the Spring-Boot-Starter-Jersey Dependency

This will add/configure the Jersey-related dependencies.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>


The Full pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.tariq</groupId>
<artifactId>Spring-Boot-Jersey-Restful-Web-Services</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name> Spring-Boot-Jersey-Restful-Web-Services Maven Webapp</name>
<url>http://maven.apache.org</url>

<properties>
<start-class>com.tariq.Application</start-class>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<packaging>war</packaging>
</properties>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.0.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>Spring-Boot-Jersey-Restful-Web-Services</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<!-- <groupId>org.codehaus.mojo</groupId> -->
<!-- <artifactId>tomcat-maven-plugin</artifactId> -->
<!-- <version>1.1</version> -->
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<url>http://localhost:8080/manager/text</url>
<server>omsserver</server>
<update>true</update>
<path>/Spring-Boot-Jersey-Restful-Web-Services</path>
<username>omsmanager</username>
<password>password</password>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<inherited>true</inherited>
<configuration>
<classpathContainers>
<classpathContainer>org.eclipse.jdt.launching.JRE_CONTAINER</classpathContainer>
<classpathContainer>org.eclipse.jdt.USER_LIBRARY/TOMCAT_6.0.14_LIBRARY</classpathContainer>
</classpathContainers>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>

</project>


Java classes:
  • Main class
  • POJO model class
  • Service
  • Jersey configuration
  • Endpoint 
Main Class:

@SpringBootApplication
public class Application extends SpringBootServletInitializer {
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}
POJO model class:

public class User {
    private String name;
    private int age;
    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
}
Service:

@Service
public class UserService {
    private Map<String, User> users;
    @PostConstruct
    private void loadUser() {
        users = new HashMap<>();
        users.put("1", new User("Tariq", 18));
        users.put("2", new User("Anseri", 29));
    }
    public User findById(String id) {
        return users.get(id);
    }
 
Jersey configuration:

@Component
@ApplicationPath("/rest")
public class ServiceConfig extends ResourceConfig {
    public ServiceConfig() {
    }
    @PostConstruct
    public void setUp() {
        packages("com.tariq.rest.controller");
        register(ObjectMapperContextResolver.class);
    }
    @Provider
    public static class ObjectMapperContextResolver implements
            ContextResolver<ObjectMapper> {
        private final ObjectMapper mapper;
        public ObjectMapperContextResolver(ObjectMapper mapper) {
            this.mapper = mapper;
        }
        @Override
        public ObjectMapper getContext(Class<?> type) {
            return mapper;
        }
    }
}

 
Endpoint :

@Component
@Path("/users")
public class UserController {
    private UserService userService;
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public User getEventVersion1(@PathParam("id") String id) {
      return userService.findById(id);
    }
}


Enjoy!!!!!!!!!!!!!!!!!!!!!!




Comments

Popular posts from this blog

How to find the companyId in Liferay.

How to add portlet in liferay theme.

How to create soap client for ofbiz service or ofbiz service invocation from another application.