Skip to main content

Spring REST vs Jersey



Every Java developer faces confusion between Spring Rest and Jersey. This is tabular comparison between both.



Spring REST
Jersey
Introduction Spring MVC stands for Model View Controller. It helps in building flexible and loosely coupled web applications. RESTful functionality is added feature Spring MVC itself Jersey RESTful Web Services framework is open source, production quality, and framework for developing RESTful Web Services in Java
Support for JAX-RS API Spring MVC does not adhere to the JAX-RS principles Jersey provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation
Supported format Spring MVC supports JSON/XML/HTML format Provides Viewable/Template to support MVC, but it is a Jersey-specific feature and not a part of JAX-RS specification
Front Controller Front Controller design pattern with the DispatcherServlet from Spring MVC Front Controller design pattern with the ServletContainer  from Jersey.

Resources



Spring MVC does not have the notion of root resources and sub-resources. Hence every controller is managed by Spring and not by the application Root resources are instantiated by the JAX-RS runtime and Sub-resources on the other hand are instantiated by the application

Web Layer Component Scope


In Spring MVC controllers are always created as singletons. They accept request-specific data as method arguments In JAX-RS, Resources are declared with per-request semantics. This is the default recommendation of JAX-RS. It enables the injection and storing of request-specific data (request headers, query parameters, cookie values) in the resource class itself. This applies to root-level resources, which are managed by JAX-RS. Sub-resources are instantiated by the application and do not benefit directly from this.

Request Parameters, Cookies, and HTTP Headers



@RequestMapping(method=GET)
public void doCall (
@RequestParam("q") String q, @CookieValue("c") String c, @RequestHeader("h") String h) {
// Spring MVC
}

@GET @Path
public void doCall(
@QueryParam("q") String q
,@FormParam("f") String f
, @CookieParam("c") String c,
@HeaderParam("h") String h
, @MatrixParam("m") m) {
// JAX-RS
}

Response


When a controller method or (its return type) is annotated with @ResponseBody, its return value is processed with an HttpMessageConverter and then used to set the response of the body. The same set of HttpMessageConverter types used for request body arguments is also used for the body of the response JAX-RS will find an entity provider of type MessageBodyWriter that can convert the object to the required content type. JAX-RS implementations are required to have a JAXB MessageBodyWriter. Custom MessageBodyWriter implementations annotated with @Provider may also be provided

Exception Handling


Spring MVC allows defining controller-level methods for dealing with exceptions .If any controller methods throw JPA's NoResultException, the above handler method will be invoked to handle it and return a 404 error. That allows each controller to deal with exceptions as it sees fit from a single place.

@Controller
@RequestMapping("/accounts")
public class AccountController {

@ResponseStatus(NOT_FOUND)
@ExceptionHandler({NoResultException.class})
public void handle() {
// ...
}
}
JAX-RS allows resource methods to throw exceptions of type WebApplicationException, which contain a Response. Instances of WebApplicationException encapsulate the logic necessary to produce a specific response but the exceptions must be caught in each individual resource class method.


@GET
@Path("{username}")
public Account getAccount(@PathParam("username") String username) {
try {
return accountRepository.findAccountByUsername(username);
} catch (NoResultException e) {
throw new NotFoundException();
}
}

Comments

Popular posts from this blog