Backend Development 8 min read

How to Integrate JAX‑RS (Jersey) into Spring Boot 2.4: Full Configuration Guide

Learn how to enable JAX‑RS (Jersey) support in Spring Boot 2.4.12 by adding the starter dependency, defining custom endpoints, configuring ResourceConfig, and managing servlets, filters, and listeners, including advanced customizations of embedded servlet containers such as Tomcat, Jetty, and Undertow.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
How to Integrate JAX‑RS (Jersey) into Spring Boot 2.4: Full Configuration Guide

Environment: Spring Boot 2.4.12

JAX‑RS (Jersey) Support

JAX‑RS is a Java EE6 technology, the Java API for RESTful Web Services, allowing creation of RESTful web services using annotations introduced in Java SE5.

Jersey is an open‑source production‑grade framework that implements the JAX‑RS API (JSR 311, JSR 339) and provides additional utilities to simplify RESTful service and client development.

If you prefer the JAX‑RS programming model for REST endpoints, you can use Jersey or Apache CXF instead of Spring MVC. Jersey has native Spring support and Spring Boot provides auto‑configuration and a starter for it.

Steps to use in Spring Boot

Add the starter dependency:

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

Create a custom endpoint:

<code>@Component
@Path("/hello")
public class Endpoint {
  @GET
  public String message() {
    return "Hello";
  }
}
</code>

Configure the resource:

<code>@Component
public class JerseyConfig extends ResourceConfig {
  public JerseyConfig() {
    register(Endpoint.class);
  }
}
</code>

After this configuration you can access the endpoint at http://<host>:<port>/hello . Because the endpoint is a Spring @Component , you can inject dependencies with @Autowired or external values with @Value . By default Jersey servlet is mapped to /* ; you can change the mapping by adding @ApplicationPath to the ResourceConfig .

<code>@Component
@ApplicationPath("/api-a")
public class JerseyConfig extends ResourceConfig {
  public JerseyConfig() {
    register(Endpoint.class);
  }
}
</code>

Servlets, Filters, Listeners

When using an embedded servlet container, you can register Servlet , Filter , and Listener beans or scan for servlet components. A bean of type Servlet , Filter , or ServletContextListener is automatically registered with the container.

If the context contains a single servlet bean, it is mapped to / . With multiple servlet beans, the bean name is used as the path prefix. Filters are mapped to /* . Example registration:

<code>@Configuration
public class ServletConfig {
  @Bean
  public Servlet1 s1() {
    return new Servlet1();
  }

  // Bean name becomes the path if not explicitly set
  // http://<host>:<port>/servlet1
  @Bean
  public ServletRegistrationBean<Servlet1> servlet1(Servlet1 s1) {
    ServletRegistrationBean<Servlet1> servlet = new ServletRegistrationBean<>(s1);
    return servlet;
  }
}
</code>

You can register servlets, filters, and listeners using ServletRegistrationBean , FilterRegistrationBean , and ServletListenerRegistrationBean respectively.

Filters are unordered by default; to enforce order, annotate the filter class with @Order or implement Ordered . If you cannot modify the filter class, define a FilterRegistrationBean and set its order via setOrder(int) . Avoid using the highest precedence for filters that read the request body, as it may interfere with character‑encoding configuration.

Servlet Context Initialization

The embedded container does not invoke javax.servlet.ServletContainerInitializer or Spring’s org.springframework.web.WebApplicationInitializer . To run initialization code, register a bean that implements org.springframework.boot.web.servlet.ServletContextInitializer and override its onStartup method.

Using @ServletComponentScan enables automatic registration of classes annotated with @WebServlet , @WebFilter , and @WebListener .

ServletWebServerApplicationContext

Spring Boot uses a specialized WebApplicationContext called ServletWebServerApplicationContext , which looks for a single ServletWebServerFactory bean (e.g., TomcatServletWebServerFactory , JettyServletWebServerFactory , or UndertowServletWebServerFactory ) to bootstrap the embedded server.

To programmatically customize the embedded server, implement WebServerFactoryCustomizer and inject ConfigurableServletWebServerFactory . Example to change the port:

<code>@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
  @Override
  public void customize(ConfigurableServletWebServerFactory server) {
    server.setPort(9000);
  }
}
</code>

Specific factories such as TomcatServletWebServerFactory , JettyServletWebServerFactory , and UndertowServletWebServerFactory expose additional setters. Example customizing Tomcat connector:

<code>@Component
public class TomcatServerCustomizerExample implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
  @Override
  public void customize(TomcatServletWebServerFactory server) {
    server.addConnectorCustomizers(tomcatConnector -> {
      tomcatConnector.setAsyncTimeout(Duration.ofSeconds(20).toMillis());
      tomcatConnector.setURIEncoding("UTF-8");
      tomcatConnector.setPort(8088);
    });
  }
}
</code>

Finished!

Spring BootRESTEmbedded ServerJAX-RSJerseyServlets
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.