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">


	<description>Demo project for Spring Boot</description>

		<relativePath/> <!-- lookup parent from repository -->






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;

public interface UsersResource {
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getAllUsers();
	@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;

public class JerseyConfig extends ResourceConfig {

	public JerseyConfig(ObjectMapper objectMapper) {
		// register endpoints
		// register jackson for json
		register(new ObjectMapperContextResolver(objectMapper));

	public static class ObjectMapperContextResolver implements ContextResolver<ObjectMapper> {
		private final ObjectMapper mapper;
		public ObjectMapperContextResolver(ObjectMapper mapper) {
			this.mapper = mapper;

		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.