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

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

Top comments (0)