This article mainly studies the relevant content of SpringMVC's support for RESTfull, as follows.
RESTful architecture is a popular Internet software architecture. It is clearly structured, compliant with standards, easy to understand and easy to expand, so it is being adopted by more and more websites. The RESTful architecture specifies urls. What does urls in RESTful format look like? The URL we request generally looks like this:
http://...../xxx.action?id=001&type=aaa
And what does REST's url style look like? Generally it is similar to:
http://..../xxx/001
Therefore, REST has a very obvious feature: make the url concise and pass the parameters to the server through the url. SpringMvc also supports this REST-style URL. Let's define a controller to test it:
//Query product information, output json, use RESTful@RequestMapping("/itemsView/{id}")public @ResponseBody ItemsCustom itemsView(@PathVariable("id") Integer id) throws Exception { ItemsCustom itemsCustom = itemsService.findItemsById(id); return itemsCustom;} @ResponseBody is an annotation used to convert itemsCustom to json, while @PathVariable annotation is related to REST. The {id} in @RequestMapping(value=”/ itemsView/{id}”) represents a placeholder. Then the value passed here will be passed to the formal parameter marked by @PathVariable . If the formal parameter is the same as the variable in the placeholder, you can no longer specify it in the annotation, otherwise the variable (i.e., id) in this placeholder should be specified in the annotation. In this way, the parameters can be passed through the url to the formal parameters.
But this is not working, and you still need to configure REST in the front-end controller, as follows:
<!-- Configure springmvc's front-end controller DispatcherServlet, REST configuration --><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>
The intercepting of all urls (/), this configuration has no conflict with the front-end controllers configured previously and can coexist. After this configuration, you can enter http://localhost:8080/SpringMVC_Study/itemsView/1 in the browser to test the data returned to the browser. You can see that a string of json data is returned.
But there is a problem. After using the above configuration, all URLs will be intercepted, and static resources will be intercepted. Therefore, the DispatcherServlet will also parse static resources, but this will cause an error, so we need to set it not to parse static resources. like:
<!-- Static resource parsing, including js,css,img... --><mvc:resources location="/js/" mapping="/js/**"></mvc:resources><mvc:resources location="/img/" mapping="/img/**"></mvc:resources>
If there are other static resources, you should also set them up so that you will not parse the static resources. You can access them directly when accessing the static resources.
The above is all the content of this article about the brief discussion of SpringMVC's support for RESTfull, and I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!