RequestMapping annotation instructions
The function of the @RequestMapping annotation maps web requests to specific handler classes and/or handler methods. This annotation can be used on a class or method and specifies the request path through the property 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 in the Controller method, it means providing a detailed URL map. If the Controller class does not have a RequestMapping annotation, the URL of the annotation mark 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 value
method: used to specify the request type: GET, POST, HEAD, OPTIONS, PUT, PATCH, DELETE, TRACE
params: Specify the requested parameters
headers: Specify the request header, source code example: RequestMapping(value = "/something", headers = "content-type=text/*")
Consumers: Specifies the content type (Content-Type) that handles the request submission, such as application/json, text/html. The request will be mapped only if the Content-Type matches one of these media types.
produces: Specify the content type returned by the request, for example: produces = "application/json; charset=UTF-8"
Specify the mapping path through the value attribute
Use RequestMapping annotation on Controller class
@Controller@RequestMapping("order") public class OrderInfoController { //Example 1 @RequestMapping("orderinfo") public ModelAndView OrderInfo1() { return new ModelAndView("order/info", "message", "OrderInfo"); }}Added the annotation RequestMapping("order") on 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 the Controller is commented out, the corresponding request path is: http://localhost:8080/springMvcNext/orderinfo
Using RequestMapping annotation on Controller method
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: Handle multiple url mappings @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 the value attribute. If no other attribute is displayed, the value is omitted. You can just fill in the URL mapping information. If other attributes are specified, the value attribute must be clearly filled in.
The access path of Example 1 above is: http://localhost:8080/springMvcNext/order/orderinfo
Example 2: The value property in the RequestMapping interface is an array, and all access paths are also supported to pass an array Example 2: http://localhost:8080/springMvcNext/order/index or http://localhost:8080/springMvcNext/order/info
Example 3: When the value is empty, it means 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 access the value from the actual request URL through the @PathVariable annotation. The example is as follows:
@Controllerpublic class OrderInfoController { // Example 10 URL with placeholder @RequestMapping(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 a request is initiated through this URL, SpringMVC will extract the ××× variable in {×××} in the URL template through @PathVariable. The URL variable will be automatically converted to the corresponding type. If it cannot be converted, an error will be returned. For example, try to access it with the following url: http://localhost:8080/springMvcNext/user/xxx/order/333 Where the parameter Userid=xxx, an error occurs:
3. Ant-style URL path mapping
Ant style wildcard characters are as follows:
Example:
@Controllerpublic class OrderInfoController { // Example 11 URL with placeholder @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 It can match all requests from http://localhost:8080/springMvcNext/order/orderXXXXX?orderNumber=yyyy
@RequestMapping(value = "order?", method = RequestMethod.GET) can match such as "…/ordera?orderNumber….” "…/orders?orderNumber….” @RequestMapping(value = "order/**", method = RequestMethod.GET) can match such as "…/order/aaa?orderNumber….” "…/order/bbb/ccc?orderNumber….”
In addition, RequestMapping also supports regular expression style URL path mapping, skip this
Specify the request type through the method attribute
The type of the 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 use @GetMapping("detail") public ModelAndView Info() { return new ModelAndView("order/info", "message", "Info"); }SpringMVC also provides dedicated annotations for each request type:
@GetMapping
@PostMapping
@PutMapping
@DeleteMapping
@PatchMapping
Specify parameter name or parameter value constraints through params
The params attribute can define that the request parameter contains specific parameters, or it can also define constraints on parameter values, as shown in the following code:
// Example 5 params The qualified parameters include orderNumber @RequestMapping(value = "detail2", params = "orderNumber") public ModelAndView Detail2(String orderNumber) { return new ModelAndView("order/info", "message", orderNumber); } // Example 6 params The qualified parameters value @RequestMapping(value = "detail3", params = "orderNumber!=1222") public ModelAndView Detail3(String orderNumber) { return new ModelAndView("order/info", "message", orderNumber); }Example 5: Qualified request parameters must contain the parameter orderNumber. If the parameter named orderNumber is not included, access is denied: Access path: http://localhost:8080/springMvcNext/order/detail2?orderNumber=12
Example 6: Qualified request parameters must contain the parameter orderNumber and the parameter value cannot be 1222. Access path: http://localhost:8080/springMvcNext/order/detail3?orderNumber=1222 An error is reported
Specify parameter name or parameter value constraints through headers
The method attribute provided by RequestMapping can specify the request header type. Only when the request data header type meets the specified value can it be accessed normally.
// Example 7 params Limited parameter value @RequestMapping(value = "headtest",headers = "apikey=2313131313") //@RequestMapping(value = "headtest",headers= {"Accept=application/json"}) public ModelAndView Header() { return new ModelAndView("order/info", "message", "Header"); }Example 7: The request header must contain apikey:23131313 to return normally, access directly, and return error:
Add header information apikey:2313131313 Access is successful:
Specify the content type of requested submission through consumption (Content-Type)
// Example 8 consumer @RequestMapping(value = "consumes", method = RequestMethod.POST, consumer = "application/json") public ModelAndView Consumers(String orderNumber) { return new ModelAndView("order/info", "message", orderNumber); }The example limits the request parameter type to application/json, which means that this method only processes requests that Content-Type is application/json:
The following passes the postman test:
Set the request parameter format to application/json, which can be accessed normally:
Set the parameter format to x-form-urlencoded, return error, Http Status 415
Specify the returned content type through produces (Content-Type)
The producers attribute is used to set the return content type and meets the following conditions: the value containing Accept in the header of the accept request is the same as the value set by the producer, or the accepted request is set without display.
// Example 8 produces limited 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 the return content format application/json. 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 accept value is not set, it can run normally
Summarize
The above is the request mapping RequestMapping annotation for Spring MVC review and learn new series of tutorials introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!