The restful architecture is one of the most popular Internet software architectures at the moment. Its structure is clear, conforms to the standard, easy to understand, the expansion is convenient, therefore is getting more and more website's adoption. RESTful (the abbreviation for representational state transfer) is actually a development idea, a good interpretation of HTTP.
1, the URL for the specification, written in restful format URL
Non-rest url:http://.../queryitems.action?id=001&type=t01
Rest URL Style: http://.../items/001
Features: URL is very concise, the parameters passed to the server via URL
2, the HTTP method to standardize
Whether it is delete, add, update .... The URL used is consistent, if you do delete, you need to set the HTTP method to delete, add ....
Background controller method: To determine the HTTP method, if it is delete to perform the deletion, if it is post to perform the add
3, the contenttype of HTTP to standardize
Specify contenttype when requested, to JSON data, set to JSON-formatted type
Let's write a simple restful example
Requirements: Query product information, return JSON data format
Definition of controller method:
Query the product information, output JSON
//@RequestMapping ("/itemsview/{id}") inside the {ID} means to pass the parameter of this position to @pathvariable the specified name
@ Requestmapping ("/itemsview/{id}") public
@ResponseBody Itemscustom Itemsview (@PathVariable ("id") Integer ID) Throws exception{
//Call service Query commodity information
itemscustom Itemscustom = Itemsservice.finditemsbyid (ID);
return itemscustom;
}
Configuring rest front-end controllers, configuring in Web.xml
<!--Configuring the rest front-end controller-->
<servlet>
<servlet-name>springmvc_rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
< init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath :spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping >
<servlet-name>springmvc_rest</servlet-name>
<url-pattern>/</url-pattern >
</servlet-mapping>
There is a problem with resolving static resources because you specify "/" in the Url-partten for configuring the front-end controller. Therefore, you need to add a method to resolve static resources in Springmvc.xml
<!--analysis of static resources
include: JS, CSS, img ....
-->
<!--access to the beginning of JS, map to the JS path of all Files img and other similar-->
<mvc:resources location= "/js/" mapping= "/js/*" * "/>
<mvc:resources location="/img/mapping= "/img/**"/>
Note: URL template pattern Mapping
@RequestMapping (value= "/viewitems/{id}"): {XXX} placeholder, the requested URL can be "VIEWITEMS/1" or "VIEWITEMS/2" by using the @pathvariable in the method Note Gets the XXX variable in {XXX}.
@PathVariable: is used to map template variables in the request URL to the parameters of the feature processing method.
The {ID} inside the @RequestMapping ("/itemsview/{id}") indicates that the parameter of this position is passed to @pathvariable in the specified name
@RequestMapping ("/itemsview/{id} /{type} ") public
@ResponseBody Itemscustom Itemsview (@PathVariable (" id ") Integer ID, @PathVariable (" type ") String Type) throws exception{}