fbpx

Spring Boot Jersey Example

Spring Boot Jersey Example:-

Today we are going to learn about configuring Spring Boot Jersey with example. We can create Rest services in Spring using spring @RestController, then Why do we want to integrate Jersey in Spring Boot. Let’s try and understand this first.

You must be knowing about JAX-RS. JAX-RS is a specification for implementing REST web services in Java, currently defined by the JSR-370. Jersey is the pure reference implementation of JAX-RS. Spring is a framework itself having Rest support.Spring RestController is not a JAX-RS reference implementation, but it’s an alternative. Spring Rest is not a core implementation of JAX-RS.




Project Structure:-

Spring Boot And Jersey




Project Dependencies:-

Declare spring-boot-starter-jersey as your dependency, as we use Jersey in our project.

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.frugalis</groupId>
	<artifactId>SpringBoot-Jersey</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>SpringBoot-Jersey</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.10.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-jersey</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</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>



Configure Spring Boot Jersey Rest Endpoints:-

package com.frugalis.resources;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.frugalis.beans.Users;

@Path("/users")
public interface UsersResource {
	@GET
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getAllUsers();
	
	@POST
	@Produces({ MediaType.APPLICATION_JSON })
	public Response saveUser( Users user);
	}

Configure Spring Boot Jersey:-

Create a file JerseyConfig.java and add the following piece of code. Here we are configuring /api as the base context of the web service. We are going to register all jersey based resources under package com.frugalis.resources  so that we can access it from our spring boot application .We have already created one above (UsersResource )which is under  package com.frugalis.resources .  Hence we need not explicitly register that resource.

package com.frugalis.SpringBootJersey;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;


@Component
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig {

	@Autowired
	public JerseyConfig(ObjectMapper objectMapper) {
		// register endpoints
		packages("com.frugalis.resources");
		// register jackson for json
		
		register(new ObjectMapperContextResolver(objectMapper));
	}

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

In the end, the difference in choosing between them usually comes down as  Are you already or do you plan to integrate any other Spring libraries into your application? If so Spring REST is the way to go as you’ll have a much easier time integrating it, otherwise, it is really just personal preference which you’d prefer to use.

If your project has chances of getting migrated to different frameworks in near future then Jersey is the way to go. Personally, I like Jersey but the power of other related Spring projects makes spring a good choice as well.