loading gif

Comparison between JAX-RS and Spring MVC

Author: Gokan EKINCI
First publication date: 2016-02-15
License: CC BY-NC-SA

You want to choose a RESTful implementation for your Java application? You want to make a comparison between JAX-RS and Spring MVC classes and annotations? Then this article is for you.

JAX-RS Spring MVC
Specification Java EE standard (JSR-311, JSR-339, JSR-370) Spring’s RESTful implementation
Package summary Java EE Javadoc Spring Javadoc
RESTful class Indicate your RESTful classes’ package name in web.xml Spring 4 : Add @RestController annotation on your RESTful class. Note: @RestController is equivalent to @Controller + @ResponseBody.
Spring 3 : Add @Controller annotation on your RESTful class.
RESTful URI @Path @RequestMapping
Consumable media type Use MediaType in @Consumes Use MediaType in @RequestMapping’s `consumes` attribute.
Producible media type Use MediaType in @Produces Use MediaType in @RequestMapping’s `produces` attribute.
Path parameter @PathParam @PathVariable
Query parameter @QueryParam @RequestParam
Default value for query parameter or path parameter (JAX-RS only for the latter) @DefaultValue Use @RequestParam’s `defaultValue` attribute.
Request type @GET, @POST, @PUT, @DELETE Use RequestMethod (enum) in @RequestMapping's `method` attribute
Using custom objects You cannot. Use a String type parameter, and then marshalize/unmarshalize it with Gson or Jackson. Add « <mvc:annotation-driven /> » in your Spring application context file then use @ResponseBody and @RequestBody annotations on your classes and methods. This implicitly uses Jackson dependency for JSON serialization.