Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between JAX-RS and Spring Rest

People also ask

What is the difference between JAX-RS and spring REST?

JAX-RS is only a specification and it needs a compatible implementation to be used. On the other hand, Spring MVC is a complete framework with REST capabilities. Like JAX-RS, it also provides us with useful annotations to abstract from low-level details.

Can I use JAX-RS with spring?

However, JAX-RS can be used with Spring (for DI, AOP, ...), too.

What is the difference between JAX-RS and JAX-WS?

Actually,JAX-WS represents both RESTful and SOAP based web services. One way to think about it is that JAX-RS specializes in RESTful, while JAX-WS allows you more flexibility to choose between either, while at the same time being (in some cases) more complicated to configure. Thank you for simple explanation.

What is difference between Jersey and spring REST?

Jersey is the JAX-RS API example implementation provided by Sun, while Spring REST is of course Spring's implementation of the same API/JSRs. The major difference is that Spring REST easily integrates into other Spring APIs (if you wish) such as Spring Data Rest.


JAX-RS

JAX-RS is a specification for implementing REST web services in Java, currently defined by the JSR-370. It is part of the Java EE technologies, currently defined by the JSR 366.

Jersey (shipped with GlassFish and Payara) is the JAX-RS reference implementation, however there are other implementations such as RESTEasy (shipped with JBoss EAP and WildFly) and Apache CXF (shipped with TomEE and WebSphere).

Spring Framework

The Spring Framework is a full framework that allows you to create Java enterprise applications. The REST capabilities are provided by the Spring MVC module (same module that provides model-view-controller capabilities). It is not a JAX-RS implementation and can be seen as a Spring alternative to the JAX-RS standard.

The Spring ecosystem also provides a wide range of projects for creating enterprise applications, covering persistence, security, integration with social networks, batch processing, etc.

Examples

Consider the following resource controller using the JAX-RS API:

@Path("/greetings")
public class JaxRsController {

    @GET
    @Path("/{name}")
    @Produces(MediaType.TEXT_PLAIN)
    public Response greeting(@PathParam("name") String name) {

        String greeting = "Hello " + name;
        return Response.ok(greeting).build();
    }
}

The equivalent implementation using the Spring MVC API would be:

@RestController
@RequestMapping("/greetings")
public class SpringRestController {

    @RequestMapping(method = RequestMethod.GET,
                    value = "/{name}", 
                    produces = MediaType.TEXT_PLAIN_VALUE)
    public ResponseEntity<?> greeting(@PathVariable String name) {

        String greeting = "Hello " + name;
        return new ResponseEntity<>(greeting, HttpStatus.OK);
    }
}

Using Spring Boot and Jersey

Spring Boot provides the spring-boot-starter-jersey module that allows you to use the JAX-RS programming model for the REST endpoints instead of Spring MVC. It works quite well with Jersey 2.x.

For a complete example of creating a web application with Jersey 2.x and Spring Boot 1.4.x, refer to this answer.


Annotation differences

(As of 2018) Spring MVC has not standardized to JAX-RS annotations, since its solution predates JAX-RS. Here are the equivalents:

enter image description here

https://stormpath.com/blog/jax-rs-vs-spring-rest-endpoints

If you are using non-standardized APIs you should expect them to be deprecated and possibly superseded by a newer experimental API in a few years. There is a lot less accountability to backward compatibility (eg when a new JDK versions get released).


I worked with both Jersey Rest, spring rest and Jersey Rest with spring. Both of them are very rich frameworks with nice implementations. I would suggest it's better to go with Spring rest if you are using other Spring services such as ORM ,Spring security and DI etc. Both are spring libraries, so I feel a little bit easy for managing code and dependencies

JAX-RS pros:

  • JSR standard can be run without servlet container (grizzly, simple, ...)
  • Production-ready implementations (jersey, cxf, resteasy, restlet, ...) designed for REST applications only

Spring MVC pros:

  • Provide "full" stack, not just REST facilities

  • Dependency injection / AOP / Transactions

  • Pluggable view templates (JSP, freemarker, velocity, ...)

You can check more on the following links

  1. https://www.infoq.com/articles/springmvc_jsx-rs
  2. Why use JAX-RS / Jersey?

JAX-RS is the specification and jersey etc are are its implementation. People use Spring to make RestFul web Services as because spring along with restful implementation provides stuff like hibernate integration and also stuff like IOC and Aspect orientated programming .

Where as if we use jersey for our implementation the problem will be the data has to fetched from the back end using some ORM technologies and we will have to write boilerplate code for the same.

That is the reason people and even enterprises use spring as along with Rest implementation it provides Spring facilities too . And now using the latest Spring boot implementation we can start development very fast without lots of configurations.