Springmvc Study Notes (19)-RESTful support
Springmvc Study Notes (19)-RESTful support
This document introduces the concept of RESTful, uses a small example to show how to write a RESTful controller and configure a front-end controller, and finally shows the analysis of static resources.
Concept
RESTful architecture is currently the most popular Internet software architecture. It has a clear structure, complies with standards, is easy to understand, and is easy to expand, so it is being adopted by more and more websites.
RESTful (short for Representational State Transfer) is a development concept and a good interpretation of http.
1. Standardize the url and write a RESTful url
Non-REST url:
http://...../queryItems.action?id=001&type=T01REST url style:
http://..../items/001
Feature: the url is concise, and parameters are uploaded to the server through the url
2. http method specification
Delete, add, update, and so on. The url is consistent. If you delete a file, you must set the http Method to delete.
Controller Method in the background: Determine the http method. If it is delete, delete it, or add it in post.
3. http contentType Specification
The contentType is specified during the request. json data is required and the type is set to json format.
REST example
Query product information and return json data.
Controller
Define method, perform url ing, and use a REST-style url to pass the id of the queried item information to the controller.
Use output json@ResponseBodyOutput the java object to json.
// Query product information. The {id} in the output json // itemsView/{id} represents a placeholder. Obtain the parameters in the placeholder through @ PathVariable, // @ PathVariable the name must be consistent with the placeholder, and the parameter name does not need to be consistent with the placeholder. // if the name in the placeholder is consistent with the parameter name, in @ PathVariable, you can not specify the name @ RequestMapping ("/itemsView/{id}") public @ ResponseBody ItemsCustom itemsView (@ PathVariable ("id") Integer items_id) throws Exception {// call the service to query the item information ItemsCustom itemsCustom = itemsService. findItemsById (items_id); return itemsCustom ;}
@RequestMapping(value="/ itemsView/{id}"):{×××}Placeholder. The requested URL can be/viewItems/1Or/viewItems/2By using@PathVariableObtain the xxx variable in {×××.@PathVariableThe template variable in the request URL is mapped to the parameter of the function processing method.
If@RequestMappingIs/itemsView/{id}, Id is the same as the parameter name,@PathVariableYou do not need to specify a name.
Frontend controller configuration of the REST Method
Springmvc_rest
Org. springframework. web. servlet. DispatcherServlet
ContextConfigLocation
Classpath: spring/springmvc. xml
Springmvc_rest
/
Access results
Static resource Parsing
Specify/The parsing of static resources will fail, and the Error 404 will be reported.
Add a static resource Parsing Method to springmvc. xml.
Accesshttp://localhost:8080/ssm1/js/jquery-1.4.4.min.js, You can see the js content in the browser