简体   繁体   中英

Some clarification about how Spring MVC use the @RequestMapping annotation to implement RESTfull architecture

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM