DEV Community

Cover image for Truly there is no difference in implementation. Integrating Spring with Jersey and integrating Jersey with Spring
Hamdamboy
Hamdamboy

Posted on

1 1

Truly there is no difference in implementation. Integrating Spring with Jersey and integrating Jersey with Spring

More interesting and arguably references are Jersey and Spring, especially integrating Jersey to Spring, or differ. This article we might response that curiosity. Catch up source code which we implemented Spring and Jersey.
Really there is no difference in implementation. Integrating Spring with
Jersey and integrating Jersey with Spring mean the same thing as far as code
is concerned.

What is Jersey, stands for...?

Briefly inform about Jersey: The Jersey RESTful Web Services framework is open source, production quality, framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.

What is JAX-RS...?

Java API for RESTful Web Services (JAX-RS) is a Java programming language API specification that provides support in creating web services according to the Representational State Transfer (REST) architectural pattern.
JAX-RS uses annotations to simplify the development and deployment of web service clients and endpoints. JAX-RS is an official part of Java EE.

This article assumes you understand Jersey, and are thinking about integrating Spring into your Jersey application. Figure-1. Three-tier application architecture.

Alt Text

Figure-1. Three-tier application architecture.

In this example, the business layer and persistence layer may seem redundant, but in a real application, the service layer also handles the business logic of the domain, not just a simple “find data”, but also “do manipulate data”. The persistence layer should only be concerned with database interactions, and not with any business logic.
In a REST application, there is no presentation layer. But does that mean that this architecture doesn’t apply to REST applications? Absolutely not. We should still adhere to this separation of concerns in REST applications. It is just good design.
With Spring, its REST layer is implemented in its MVC framework. The MVC framework is widely know for its MVC capabilities with the use of controllers. But with a little tweaking of the controllers and the annotations used, the controllers can easily become a REST controller, where instead of return models and views, you are return RESTful representation objects. More valuable information adhere Figure-2. A multi-layered architecture, based on the “Law of Demeter".

Alt Text
Figure-2. Multi-layer architecure.

  • the first layer is the REST support implemented with Jersey, has the role of a facade and delegates the logic to the business layer
  • the business layer is where the logic happens
  • the data access layer is where the communcation with the pesistence storage (in our case the MySql database) takes place

Source code:

 @SpringBootApplication
 public class JerseyApplication implements WebApplicationInitializer {

  public static void main(String[] args) {
    SpringApplication.run(JerseyApplication.class, args);
  }

  @Override
 public void onStartup(ServletContext sc) throws ServletException {
    sc.setInitParameter("contextConfigLocation", "noop");

    AnnotationConfigWebApplicationContext context
            = new AnnotationConfigWebApplicationContext();
    context.register(SpringConfig.class);
    sc.addListener(new ContextLoaderListener(context));
    sc.addListener(new RequestContextListener());
 }
Enter fullscreen mode Exit fullscreen mode

Reference:

  1. WHY AND HOW TO USE SPRING WITH JERSEY?
  2. Tutorial – REST API design and implementation in Java with Jersey and Spring

Heroku

Deploy with ease. Manage efficiently. Scale faster.

Leave the infrastructure headaches to us, while you focus on pushing boundaries, realizing your vision, and making a lasting impression on your users.

Get Started

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • --last-failed: Zero in on just the tests that failed in your previous run
  • --only-changed: Test only the spec files you've modified in git
  • --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Practical examples included!

Watch Video 📹️

Introducing RTABench

To meet the need for real-time analytics workloads, we developed RTABench, a benchmark designed to test databases with these specific requirements. RTABench focuses on essential query patterns such as joins, filtering, and pre-aggregations.