Backend Development 10 min read

Mastering Jersey RESTful Integration with Spring Boot: A Complete Guide

This guide explains how to integrate the Jersey RESTful framework with Spring Boot, covering its relationship to JAX‑RS, configuration steps, essential annotations, code examples, and advanced customizations such as ResourceConfig, filters, servlets, and application path settings.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Mastering Jersey RESTful Integration with Spring Boot: A Complete Guide

Overview

Jersey RESTful framework is an open‑source implementation of the JAX‑RS (JSR 311 & JSR 339) specifications. It extends the reference implementation, offering additional features and tools that simplify RESTful service and client development. Although relatively young, it is production‑ready and can be integrated with Hibernate, Spring, and other frameworks.

Jersey 1.x uses Sun's com.sun.jersey package, while Jersey 2.x uses GlassFish's org.glassfish.jersey package.

Relationship between Jersey and JAX‑RS: JAX‑RS is a lightweight Java specification for RESTful services, whereas Jersey is a more mature framework built on top of JAX‑RS, reusing its annotations and adding its own powerful features.

If you prefer the JAX‑RS programming model, you can use Jersey or Apache CXF instead of Spring MVC. Both work out of the box, and Jersey has native Spring support via an auto‑configuration starter.

To start using Jersey, add the spring-boot-starter-jersey dependency and define a ResourceConfig bean to register your endpoints.

Example Configuration

<code>@Component
public class MyJerseyConfig extends ResourceConfig {
  public MyJerseyConfig() {
    register(MyEndpoint.class);
  }
}
</code>
<code>@Component
@Path("/hello")
public class MyEndpoint {
  @GET
  public String message() {
    return "Hello";
  }
}
</code>
Jersey has limited support for scanning executable archives. When running an executable WAR, it cannot scan packages inside a fully executable JAR or WEB-INF/classes . To avoid this limitation, use the register method instead of packages for endpoint registration.

For advanced customization, you can register any number of beans implementing ResourceConfigCustomizer . All registered endpoints must be annotated with HTTP resource annotations such as @GET and marked as @Component .

<code>@Component
@Path("/users")
public class UserEndpoint {
  @Path("/{id}")
  @GET
  @Produces({"application/json"})
  public User queryUser(@PathParam("id") Integer id) {
    return new User(id, "Name - " + id, 6 + id);
  }
}
</code>

JAX‑RS Annotations

@Path : Defines the URI path for a class or method. Supports template variables (e.g., @Path("/{id}") ) and regular expressions (e.g., @Path("{id: \d+}") ).

@Method (e.g., @GET , @POST , @PUT , @PATCH , @DELETE ): Determines the HTTP verb that a method handles.

@Produces : Specifies the media types a resource can produce. Examples: @Produces("text/plain") , @Produces("application/json") , or multiple types such as {"application/xml", "application/json"} .

@Consumes : Specifies the media types a resource can consume. If omitted, any media type is accepted.

@QueryParam vs @PathParam : @QueryParam extracts query‑string parameters (e.g., /api/users?id=1 ), while @PathParam extracts values from the URI path (e.g., /api/users/1 ).

Jersey Detailed Configuration in Spring Boot

Add the Maven dependency:

<code>&lt;dependency&gt;
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-jersey&lt;/artifactId&gt;
&lt;/dependency&gt;
</code>

Define a bean that extends ResourceConfig . The auto‑configuration activates only when a ResourceConfig bean is present.

<code>@Configuration(proxyBeanMethods = false)
@ConditionalOnBean(type = "org.glassfish.jersey.server.ResourceConfig")
@EnableConfigurationProperties(JerseyProperties.class)
public class JerseyAutoConfiguration implements ServletContextAware {
  // beans for ServletContainer or FilterRegistration are created here
}
</code>

The auto‑configuration registers either a ServletContainer or a filter. By default (when spring.jersey.type is not set to filter ), a servlet is used with the mapping /* .

Customizing the ResourceConfig can be done by providing beans that implement ResourceConfigCustomizer :

<code>@Component
public class CustomResourceConfigCustomizer implements ResourceConfigCustomizer {
  @Override
  public void customize(ResourceConfig config) {
    // custom configuration logic
  }
}
</code>

Set a common API prefix with @ApplicationPath :

<code>@Component
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig {
}
</code>

Or configure it via application.yml :

<code>spring:
  jersey:
    type: servlet
    application-path: /api-a
</code>

Full endpoint example:

<code>@Component
@Path("/users")
public class UserEndpoint {
  @Path("/{id}")
  @GET
  @Produces({"application/json"})
  public User queryUser(@PathParam("id") Integer id) {
    return new User(id, "Name - " + id, 6 + id);
  }
}
</code>

End of guide.

BackendJavaSpring BootRESTJAX-RSJersey
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.