[Go] build restful web apps with spring MVC

Source: Internet
Author: User
Tags http post representational state transfer

Original address: http://fancy888.iteye.com/blog/1629120

For the MIS system running on the network, processing the data is the main task of the whole system, we can see that over 80% of the code is processing the crud operation of the data. The use of a framework structure that makes processing data easy and understandable is an issue that most developers, especially architects, have been thinking about.

Rest (representational state Transfer) is a programming style, especially designed and developed for Web applications, and using rest can significantly reduce the complexity of development and increase the scalability of the system.

Rest is a unique programming style, unlike the MVC emphasis on the role layering concept, rest emphasizes the use of uniform rules to standardize the operation of the resources, the two are complementary, consistent with each other, using the MVC Architecture collocation Rest Programming Style Network-based MIS system will be not far from the mainstream. This article focuses on the core rules of the rest programming style, and combines spring MVC to describe the code morphology of rest in real-world applications.

Developing Web applications using restful styles emphasizes the following rules:

    1. All things on the network are abstracted as resources (resource);
    2. Each resource corresponds to a unique resource identifier (resource identifier);
    3. Operation of the resource via a common connector interface (generic connector interface);
    4. The various operations on resources will not change the resource identification;
    5. All operations are stateless (stateless).

First, in my opinion, the resources in the rest definition are the data that the application processes. For example, the order management system, the order information is the data, the same customer information and product information is also the data, in the rest it seems that the data are resources.

Rest emphasizes that a resource must have a unique name and ID, which is used to differentiate between different resources, and the ID is used to locate only one data in a homogeneous resource. In rest, these rules are described by URIs. Or take the order system above as an example, we set the order information name to order, customer information for customers, goods information for product, then there are several URI design:

HTML code
    1. <!--identify all order information--
    2. Http://www.coolfancy.com/rest/order
    3. <!--identify all customer information--
    4. Http://www.coolfancy.com/rest/customer
    5. <!--ID 1 Cargo Information--
    6. Http://www.coolfancy.com/rest/product/1

We can see the features of the rest style URI design: http://host/context/Resource name/[resource ID]. Where the resource name is a required option, the resource ID can be omitted, and the resource ID is used to identify all data for a class of resources.

With a resource and a URI to identify the resource, rest uses the connector to identify the operation on the resource. Here, the operation of the resource is divided into query/get, create, delete, modify four kinds. In a network programming environment, rest uses the HTTP protocol as the connector, using the HTTP method (request method) to identify the type of operation:

    • HTTP get: Querying and retrieving resources;
    • HTTP POST: Creating resources;
    • HTTP PUT: modifying resources;
    • HTTP Delete: Deletes a resource.

After completing the definition of two core concepts of resources and connectors, we have been able to outline the basic ideas of rest-style programming:

    1. Abstract and define resources in the system;
    2. Use a restful URI to bind the request to the resource;
    3. Use HTTP requests to manipulate resources;

Also use the order system mentioned above for example, please see the following example:

HTML code
    1. <!--search for goods in the category of clothing information--
    2. Get/rest/product http/1.1
    3. Host:www.coolfancy.com
    4. ProductType Clothing

HTML code
    1. <!--query for order information with ID 1-
    2. GET/REST/ORDER/1 http/1.1
    3. Host:www.coolfancy.com

HTML code
    1. <!--delete cargo information with ID 1--
    2. DELET/REST/PRODUCT/1 http/1.1
    3. Host:www.coolfancy.com

HTML code
    1. <!--Create an order information--
    2. Post/rest/order http/1.1
    3. Host:www.coolfancy.com
    4. Field1 value1
    5. Field2 value2
    6. ... ...

Simply put, the URI plus HTTP method forms the core rule of rest processing data: The URI determines the object of the operation, and the HTTP method determines how it is manipulated.

Unlike traditional ways of working with data, if you do not use rest, the objects and operations of the operation will be mixed in the URL design, just as Dr. Roy T. Fielding, who first presented the rest concept, said: "Rest allows us to design the system through a URL." In fact, Roy was also the main designer of the Web protocol, and it was with his participation that he completed the formulation of the HTTP1.1 specification.

At another point of view, rest emphasizes the concept of resources (data), where all operations are carried out around a specific resource, leaving the resources, and rest loses the meaning of existence. At this point, the ideas that rest advocates are similar to the popular Ajax programming styles.

Ajax emphasizes data and presentation separation, background applications are dedicated to production data, and the foreground uses scripts to display data. There is no built-in support for the organization of data and the transfer of Ajax. While rest emphasizes binding requests to resources through URIs, if you combine rest with Ajax technology, you can create an effective complement, and we might as well extend the programming ideas mentioned above, and still take the order information with ID 1 as an example:

    1. Ajax initiation Request: HTTP://WWW.COOLFANCY.COM/REST/ORDER/1;
    2. Bind the request to the background entity through a restful URI design;
    3. The background entity returns the Json/xml data containing the product information;
    4. The foreground uses a script to display the data.

We use spring MVC to implement the process described above, and as a mainstream MVC product, Spring MVC naturally supports the rest programming style, so using spring MVC to build restful applications will become surprisingly simple.

Spring MVC uses special annotations to decorate handler objects, allowing handler to handle rest-style requests, and let's look at a few examples:

Java code
  1. /**
  2. * Get order information by ID value
  3. *
  4. * @param ID
  5. * @return
  6. */
  7. @ResponseBody
  8. @RequestMapping (value = "/order/${id}", method = Requestmethod.get)
  9. Public OrderEntity GetOrder (@PathVariable int id) {
  10. return Ordermanager.get (ID);
  11. }
  12. /**
  13. * Check cargo information by type
  14. *
  15. * @param ID
  16. * @return
  17. */
  18. @ResponseBody
  19. @RequestMapping (value = "/product", method = Requestmethod.get)
  20. Public list<productentity> Getproductbytype (String type) {
  21. return Productmanager.querybytype (type);
  22. }
  23. /**
  24. * Create order Information
  25. *
  26. * @param ID
  27. * @return
  28. */
  29. @RequestMapping (value = "/order", method = Requestmethod.post)
  30. Public void Createorder (OrderEntity entity) {
  31. Ordermanager.create (entity);
  32. }
  33. /**
  34. * Modify order Information
  35. *
  36. * @param ID
  37. * @return
  38. */
  39. @RequestMapping (value = "/order/${id}", method = Requestmethod.put)
  40. Public OrderEntity UpdateOrder (@PathVariable int ID, orderentity entity) {
  41. return Ordermanager.update (entity);
  42. }
  43. /**
  44. * Delete order information for the specified ID value
  45. *
  46. * @param ID
  47. * @return
  48. */
  49. @RequestMapping (value = "/order/${id}", method = Requestmethod.delete)
  50. Public void Deleteorder (@PathVariable int id) {
  51. Ordermanager.delete (ID);
  52. }

The example above shows how simple it is to write a restful application using spring MVC.

It is important to note that rest requires the identity of the resource to be constant, that is, for a particular resource, the ID of the resource cannot be changed, regardless of the operation on the resource. This restriction is added to ensure the consistency of the URI. Imagine that if an operation changes the identity of a resource during processing, then to ensure the integrity of the rules, we have to pay an extra price to synchronize these changes in other processes, which is unacceptable in rest.

On the other hand, rest requires that all operations on resources be stateless, and URIs are the only way to determine resources. If we incorporate state data into the process of data processing, the URI for the same resource will be two semantic, which will run counter to the rest definition.

[Go] build restful web apps with spring MVC

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.