In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of Spring MVC request mapping RequestMapping. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
RequestMapping comment description
The @ RequestMapping annotation maps Web requests to specific handler classes and / or handler methods, which can be used on classes or methods and specifies the request path through the attribute value. Used on the Controller class to provide preliminary URL request mapping information, which is a pre-request path relative to the root directory of the Web application. Used on methods in Controller to provide a detailed URL mapping. If there is no RequestMapping annotation on the Controller class, the URL marked on the method is relative to the root directory of the Web application.
The @ RequestMapping annotation provides the following properties:
Name: used to specify the mapper name
Value: used to specify the mapping path, same as path
Path: used to specify the mapping path, same as value
Method: used to specify the request type: GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE
Params: specify the parameters of the request
Headers: specify the request header. Source code example: RequestMapping (value = "/ something", headers = "content-type=text/*")
Consumes: specifies the content type (Content-Type) to process the request submission, such as application/json, text/html, and the request is mapped only if the Content-Type matches one of these media types
Produces: specifies the content type returned by the request, for example: produces = "application/json; charset=UTF-8"
Specify the mapping path through the value attribute
Using RequestMapping annotations on Controller classes
Controller@RequestMapping ("order") public class OrderInfoController {/ / example 1 @ RequestMapping ("orderinfo") public ModelAndView OrderInfo1 () {return new ModelAndView ("order/info", "message", "OrderInfo");}}
An annotation RequestMapping ("order") has been added to the OrderController class, indicating that all requests for pairs must start with "root / order"
The request path for example 1 is: http://localhost:8080/springMvcNext/order/orderinfo
Example 1 if @ RequestMapping ("order") on Controller is commented out, the corresponding request path is: http://localhost:8080/springMvcNext / orderinfo
Using RequestMapping annotations on Controller methods
1. Common basic usage
@ Controller@RequestMapping ("order") public class OrderInfoController {/ / example 1 @ RequestMapping ("orderinfo") public ModelAndView OrderInfo1 () {return new ModelAndView ("order/info", "message", "OrderInfo") } / example 2: deal with multiple url maps @ RequestMapping ({"info", "index"}) / / or @ RequestMapping (value= {"info", "index"}) public ModelAndView OrderInfo2 () {return new ModelAndView ("order/info", "message", "OrderInfo2");} / / example 3 @ RequestMapping public ModelAndView OrderInfo3 () {return new ModelAndView ("order/info", "message", "OrderInfo3");}}
RequestMapping only configures value attributes. If other attributes are not displayed, value can be omitted and URL mapping information can be filled in directly. If other attributes are specified, value attributes must be explicitly filled in.
The access path for example 1 above is: http://localhost:8080/springMvcNext/order/orderinfo
The value attribute in the sample 2:RequestMapping interface is an array, and all support passing an array access path to example 2: http://localhost:8080/springMvcNext/order/index or http://localhost:8080/springMvcNext/order/info.
Example 3: when value is empty, it indicates that the method is the default Action under the class. The access path of example 3 is: http://localhost:8080/springMvcNext/order
2.URL template mapping
Declare the URI variable in the RequestMapping annotation, and obtain the value from the actual request URL by means of @ PathVariable annotation. The example is as follows:
@ Controllerpublic class OrderInfoController {/ / example 10 URL @ RequestMapping with placeholders (value = "user/ {userId} / order/ {orderNumber}", method = RequestMethod.GET) public ModelAndView OrderInfo4 (@ PathVariable int userId,@PathVariable String orderNumber) {return new ModelAndView ("order/info", "message", "userid:" + userId+ "orderNumber:" + orderNumber);}}
Example 10 request URL: http://localhost:8080/springMvcNext/user/12/order/333 when initiating a request through this URL, SpringMVC can extract the × × variable in the URL template through @ PathVariable, and the URL variable will be automatically converted to the corresponding type, and an error will be returned if it cannot be converted. For example, if you try to access it with the following url: http://localhost:8080/springMvcNext/user/xxx/order/333 the parameter Userid=xxx, an error occurs:
URL path Mapping of 3.Ant style
The Ant style wildcards are as follows:
? Match a character
* match zero or more characters in the path segment
* * match zero or more path segments
Example:
@ Controllerpublic class OrderInfoController {/ / example 11 placeholder URL @ RequestMapping (value = "order*", method = RequestMethod.GET) / / @ RequestMapping (value = "order?", method = RequestMethod.GET) / / @ RequestMapping (value = "order/**", method = RequestMethod.GET) public ModelAndView OrderInfo5 (String orderNumber) {return new ModelAndView ("order/info", "message", "OrderInfo5");}}
Example 11 request URL: http://localhost:8080/springMvcNext/order/orderdexx?orderNumber=12 can match all requests of http://localhost:8080/springMvcNext/order/orderXXXXX?orderNumber=yyyy
@ RequestMapping (value = "order?", method = RequestMethod.GET) can match things such as "… / ordera?orderNumber …."… / orders?orderNumber …." @ RequestMapping (value = "order/**", method = RequestMethod.GET) can match "… / order/aaa?orderNumber …."… / order/bbb/ccc?orderNumber …."
In addition, RequestMapping also supports regular expression style URL path mapping, which is skipped here
Specify the request type through the method property
The type of method attribute request predicate provided by RequestMapping. The following example only accepts GET requests.
/ / example 4 @ RequestMapping (value= "detail", method=RequestMethod.GET) / / you can also directly use @ GetMapping ("detail") public ModelAndView Info () {return new ModelAndView ("order/info", "message", "Info");}
For each request type, SpringMVC also provides dedicated comments:
@ GetMapping
@ PostMapping
@ PutMapping
@ DeleteMapping
@ PatchMapping
Specify parameter name or parameter value constraints through params
The params attribute can limit request parameters to include specific parameters, or restrict parameter values, as shown in the following code:
/ / example 5 params qualifying parameter contains orderNumber @ RequestMapping (value = "detail2", params = "orderNumber") public ModelAndView Detail2 (String orderNumber) {return new ModelAndView ("order/info", "message", orderNumber);} / / example 6 params qualifying parameter value @ RequestMapping (value = "detail3", params = "orderNumbercoding qualification 1222") public ModelAndView Detail3 (String orderNumber) {return new ModelAndView ("order/info", "message", orderNumber);}
Example 5 defines that the request parameter must contain the parameter orderNumber, and if it does not include a parameter named orderNumber, access is denied: access path: http://localhost:8080/springMvcNext/order/detail2?orderNumber=12
Example 6 the request parameter must contain the parameter orderNumber and the parameter value cannot be 1222 access path: http://localhost:8080/springMvcNext/order/detail3?orderNumber=1222 Times error
Specify parameter name or parameter value constraints through headers
The method attribute provided by RequestMapping can specify the request header type, which can be accessed normally only if the request data header type conforms to the specified value.
/ / example 7 params qualifying parameter value @ RequestMapping (value = "headtest", headers= "apikey=23131313") / / @ RequestMapping (value = "headtest", headers= {"Accept=application/json"}) public ModelAndView Header () {return new ModelAndView ("order/info", "message", "Header");}
Example 7 defines that the request header must contain apikey:23131313 before it can be returned normally. It can be accessed directly and an error is returned:
Add add header information apikey:23131313 access succeeded:
Specify the type of content to be submitted through consumes (Content-Type)
/ / example 8 consumes @ RequestMapping (value = "consumes", method = RequestMethod.POST, consumes = "application/json") public ModelAndView Consumes (String orderNumber) {return new ModelAndView ("order/info", "message", orderNumber);}
The example limits the request parameter type to application/json, indicating that the method only handles requests with the request Content-Type of application/json:
Here is the test by throwing postman:
Set the request parameter format to application/json, which can be accessed normally:
Set the parameter format to x-form-urlencoded and return an error, Http Status 415
Specify the returned content type (Content-Type) through produces
The produces property is used to set the return content type and satisfies the following conditions: the value of Accept in the accept request header is the same as the value set by produces, or the accepted request uses the do not display setting alt value
/ example 8 produces qualifies the return data application/json @ RequestMapping (value = "produces", method = RequestMethod.GET, produces = "application/json") public ModelAndView Produces (String orderNumber) {return new ModelAndView ("order/info", "message", orderNumber);}
Example 8 indicates that the content format application/json is returned. When the accept format set by the client is text/json, an error is reported. Http status 406
When the accept format set by the client is application/json or the default value is not set, it can run normally.
This is the end of the article on "sample Analysis of Spring MVC request Mapping RequestMapping". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.