0

I am studying for the Spring Core certification and I have some doubt about how Spring MVC handle REST web service.

Reading the documentation I found this example:

@RequestMapping(value="/orders", method=RequestMethod.GET) public void listOrders(Model model) { // find all Orders and add them to the model } @RequestMapping(value="/orders", method=RequestMethod.POST) public void createOrder(HttpServletRequest request, Model model) { // process the order data from the request } 

Ok, it show 2 Spring MVC method (that I think should be declared into a controller class, is it true).

These methods both handle HTTP request towards the /orders resource (according to the REST style in which a resource is seen as a programming element that manages a kind of data and a state and provide processing on this kind).

In this case if the HTTP request toward the /orders is a GET it will be executed the listOrders() method that return the list of all objects but if the request toward the /orders is a POST it will perform the createOrder() that create a new order

So what exactly means, that using the method paramether of the @RequestMapping annotation I can handle the HttpRequest according to the RESTful style?

1
  • 1
    see also Spring Data REST project Commented Feb 15, 2015 at 19:57

1 Answer 1

1

REST is an architecture style that uses the various HTTP methods to model actions on resources.

Spring's @RequestMapping annotation is just a way to map a handler method to an HTTP request. The method attribute simply restricts which HTTP methods can be handled by the annotated method.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, this is clear for me. But is it thinked to bundle together a resource with a specific HTTP method (the so called "verbs")?
I don't understand your question.
In a RESTful web api that use HTTP protocol I have to use the HTTP method set (GET, PUT, POST, DELETE) on a specific resource. So my question is: it it the method=RequestMethod.XXX the proper way used by Spring MVC to handle the HTTP method (the verbs) to a resource specified by the @RequestMapping annotation?