In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Most people do not understand the knowledge points of this article "what is the use of RequestMapping annotations", so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the role of RequestMapping annotations" article.
The role of @ RequestMapping annotations
@ RequestMapping is one of the most commonly used annotations in Spring Web applications. This annotation maps the HTTP request to the processing methods of the MVC and REST controllers. And an annotation that handles the request address mapping can be used on a class or method. Used on a class, the method that represents all response requests in the class has this address as the parent path.
The following is the source code of @ RequestMapping: package org.springframework.web.bind.annotation;import java.lang.annotation.Documented;import java.lang.annotation.ElementType;import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;import java.lang.annotation.Target;import org.springframework.core.annotation.AliasFor;@Target ({ElementType.METHOD, ElementType.TYPE}) @ Retention (RetentionPolicy.RUNTIME) @ Documented@Mappingpublic @ interface RequestMapping {String name () default "" @ AliasFor ("path") String [] value () default {}; @ AliasFor ("value") String [] path () default {}; RequestMethod [] method () default {}; String [] params () default {}; String [] headers () default {}; String [] consumes () default {}; String [] produces () default {};}
Methods:
Request the second level of URL to access the directory.
Attributes:
Value: used to specify the URL of the request. It has the same effect as the path attribute.
Method: used to specify the method of the request.
Params: used to specify conditions to limit request parameters. It supports simple expressions. The key and value that require the request parameters must be exactly the same as the configuration.
For example:
Params = {"accountName"}, indicating that the request parameter must have
AccountNameparams = {"moenyroom100"}, indicating that the money in the request parameters cannot be 100.
Headers: used to specify conditions that restrict request headers.
Note:
As long as there are two or more of the above four attributes, their relationship is the relationship with.
Function:
Used to establish the correspondence between the request URL and the request processing method.
Where it appears:
Request the first level of URL to access the directory. If it is not written here, it is equivalent to the root directory of the application. If you write, you need to start with /. It appears so that our URL can be managed in a modular manner:
For example:
Account module:
/ account/add
/ account/update
/ account/delete...
Order module:
/ order/add
/ order/update
/ order/delete
Case
Common case: annotate the @ RequestMapping annotation on the class and the method respectively, so write the full path when writing the link at the front end (path on the class label + road strength on the method label)
Code block of the controller:
The location where the / * RequestMapping annotation appears * @ author * @ Company http://www.ithiema.com* @ Version 1.0*/@Controller ("accountController") @ RequestMapping ("/ account") public class AccountController {@ RequestMapping ("/ findAccount") public String findAccount () {System.out.println ("account queried.") ; return "success";}}
Code blocks in JSP
The use of requestmapping query account query account
Case: used to specify the method of the request. If the request is a get request, then the method=get of the request method is written at the front end, whereas if the request is a post request, the front end, medthod=post
Controller code block:
/ * * Save account * @ return*/@RequestMapping (value= "/ saveAccount", method=RequestMethod.POST) public String saveAccount () {System.out.println ("saved account"); return "success";}
Jsp code block
Save account, get request
Note: when using get request, the error message is 405.The message is that the method does not support get request.
Example of the params property: used to specify conditions that restrict request parameters. It supports simple expressions. The key and value that require the request parameters must be exactly the same as the configuration. After writing this property, be sure to send the request at the front end, the front end must have this property, otherwise it will not be accessible. Controller code block:
/ * * Delete account * @ return*/@RequestMapping (value= "/ removeAccount", params= {"accountName", "money > 100"}) public String removeAccount () {System.out.println (" deleted account "); return" success ";}
Jsp code block:
Delete account, amount 100 delete account, amount 150
Note:
When we click on the first hyperlink, we can access it successfully.
When we click on the second hyperlink, we cannot access it.
As shown below:
Parameters that support REST style: representative State transfer (REST) is an architectural style for distributed hypermedia systems such as the World wide Web. (the following test code is used in conjunction with @ PathVariable)
Controller code block:
@ RequestMapping (value = "/ testRequestMapping/ {Id}") public String testRequestMapping (@ PathVariable String Id) {System.out.println ("testRequestMapping executed" + Id); return "success";}
Jsp front-end code block
Detailed explanation of six attributes of @ RequestMapping test @ RequestMapping annotations
RequestMapping is an annotation that handles request address mapping and can be used on classes or methods. Used on a class, the method that represents all response requests in the class has this address as the parent path.
RequestMapping annotations have six attributes
The following is divided into three categories to explain
Value, method
Value: specify the actual address of the request, which can be a specific address, can be dynamically obtained by RestFul, or can be set by regular settings
Method: specifies the method type of the request, which is divided into GET, POST, PUT, DELETE, etc.
Consumes,produces
Consumes: specifies the submitted content type (Content-Type) to process the request, such as application/json, text/html
Produces: specifies the content type to be returned, only if the (Accept) type in the request request header contains the specified type
Params,headers
Params: specifies that some parameter values must be included in the request to be handled by this method.
Headers: specifies that some specified header value must be included in the request for this method to process the request.
The above is the content of this article on "what is the role of RequestMapping annotations". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
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.