JAX-RS define specification for REST webservice. REST principal goes very well with HTTP protocol that’s the reason people normally interpret REST as REST over HTTP service. There are many libraries are there in market which helps in creating REST services Spring Rest, JAX-RS Jersey, Apache CXF, RestEasy. All are quite a bit same and have matching syntax. In this we will discuss mainly about JAX-RS standard and Jersey implementation of JAX-RS, later little bit on Spring REST.
Spring with Jersey
Spring has it’s own rest framework and with different annotations but let’s look at JAX-RS standard J2EE JAX-RS annotation with Spring in action.
Check example above to see the annotation usage. @GET - Method decorator and handle HTTP get request. @Produces & @Consumes - Defines datatype accepted/produced by method for ex text/xml, text/JSON etc. @Path - Bind method or class to a URL path and calls it when the path is matched. @PathParam - Bind the URL matching value to method argument. @QueryParam - Binds the URL parameter to method argument, Maps GET parameters. @FormParam - Binds the POST parameters to method arguments. @HeaderParam - Binds header value to method parameters. @POST - Binds the complete post parameter to a object in method argument. It usage Java serialization and de-serialization to create Java object. @PUT & @DELETE - Put is same as post but there is slight difference in logic. DELETE is also same as POST but logically the code inside should do some deletion. @Context - Getting URI related information.
Spring Rest framework.
Spring MVC rest related annotations .
@RestController - @ResponseBody + @Controller is @RestController, It adds filter to serialize the response. @RequestMapping - Maps the URL path to class and methods, also specifies the content type of request and response. Has these fields - method, header, name, value, produces, consumes.
@RequestParam - @RequestParam maps the get post value to method argument. You can take default values as well.
@PathVariable - Path variable used to map URL path value to a method argument.
@SessionAttribute - Map session values to method argument.
@CookieValue - Map cookie value to method argument.