当前位置:网站首页>3、 @requestmapping annotation
3、 @requestmapping annotation
2022-07-26 04:27:00 【Time postman】
List of articles
- 1、@RequestMapping The function of annotation
- 2、@RequestMapping The position of the annotation
- 3、@RequestMapping Annotated value attribute
- 4、@RequestMapping Annotated method attribute
- 5、@RequestMapping Annotated params attribute ( understand )
- 6、@RequestMapping Annotated headers attribute ( understand )
- 7、SpringMVC Support ant The path of style
- 8、SpringMVC Supports placeholders in paths ( a key )
1、@RequestMapping The function of annotation
From the annotation name, we can see ,@RequestMapping The purpose of the annotation is to associate the request with the controller method that processes the request , Establish a mapping relationship .
SpringMVC The specified request was received , You will find the corresponding controller method in the mapping relationship to process the request .
2、@RequestMapping The position of the annotation
@RequestMapping Identify a class : Set the initial information of the request path of the mapping request
@RequestMapping Identify a method : Set the specific information of the mapping request path
@Controller
@RequestMapping("/test")
public class RequestMappingController {
// At this time, the request path of the request mapped by the request mapping is :/test/testRequestMapping To access success
@RequestMapping("/testRequestMapping")
public String testRequestMapping(){
return "success";
}
}
3、@RequestMapping Annotated value attribute
@RequestMapping Annotated value Property through Requested request address Match request mapping
@RequestMapping Annotated value Property is a An array of string types , Indicates that the request mapping can match Requests corresponding to multiple request addresses
@RequestMapping Annotated value Property must be set , At least match the request mapping through the request address
<a th:href="@{/testRequestMapping}"> test @RequestMapping Of value attribute -->/testRequestMapping</a><br>
<a th:href="@{/test}"> test @RequestMapping Of value attribute -->/test</a><br>
@RequestMapping(
value = {
"/testRequestMapping", "/test"}
)
public String testRequestMapping(){
return "success";
}
4、@RequestMapping Annotated method attribute
@RequestMapping Annotated method Property through How to request (get or post) Match request mapping
@RequestMapping Annotated method Property is a RequestMethod An array of types , Indicates that the request mapping can match Multiple requests
If the request address of the current request meets the requirements of the request mapping value attribute , But the request method is not satisfied method attribute , The browser reports an error 405:Request method ‘POST’ not supported
<form th:action="@{/test}" method="post">
<input type="submit">
</form>
@RequestMapping(
value = {
"/testRequestMapping", "/test"},
method = {
RequestMethod.GET, RequestMethod.POST}
)
public String testRequestMapping(){
return "success";
}
notes :
1、 For the controller method that handles the specified request mode ,SpringMVC Provided in @RequestMapping Derived annotation for
Handle get Requested mapping –>@GetMapping Handle post Requested mapping –>@PostMapping
Handle put Requested mapping –>@PutMapping Handle delete Requested mapping –>@DeleteMapping2、 The common request methods are get,post,put,delete
But at present, browsers only support get and post, If in form When the form is submitted , by method Set the string of other request methods (put or delete), The default request method is used get Handle
To send put and delete request , You have to go through spring Filter provided HiddenHttpMethodFilter, stay RESTful I'll talk about it in part
5、@RequestMapping Annotated params attribute ( understand )
@RequestMapping Annotated params Property through Requested request parameters Match request mapping
@RequestMapping Annotated params Property is a An array of string types , You can pass There are four expressions Set the matching relationship between request parameters and request mapping
“param”: The request matching the request mapping must carry param Request parameters
“!param”: The request matching the request mapping must not carry param Request parameters
“param=value”: The request matching the request mapping must carry param Request parameters and param=value
“param!=value”: The request matching the request mapping must carry param Request parameters, but param!=value
<a th:href="@{/test(username='admin')}"> test @RequestMapping Of params attribute -->/test</a><br>
@RequestMapping(value = {
"/testRequestMapping","/test"},
method = {
RequestMethod.GET, RequestMethod.POST},
params ={
"username"} )
public String testRequestMapping(){
return "success";
}
notes : If the current request is satisfied @RequestMapping Annotated value and method attribute , But not satisfied params attribute , The page returns an error 400
6、@RequestMapping Annotated headers attribute ( understand )
@RequestMapping Annotated headers Property through Request header information of the request Match request mapping
@RequestMapping Annotated headers Property is a An array of string types , The matching relationship between request header information and request mapping can be set through four expressions
“header”: The request matching the request mapping must carry header Request header information
“!header”: The request matching the request mapping must not carry header Request header information
“header=value”: The request matching the request mapping must carry header Request header information and header=value
“header!=value”: The request matching the request mapping must carry header Request header information and header!=value
@RequestMapping(value = {
"/testRequestMapping","/test"},
method = {
RequestMethod.GET, RequestMethod.POST},
params ={
"username"},
headers={
"Host=localhost:8080"})
public String testRequestMapping(){
return "success";
}
If the current request is satisfied @RequestMapping Annotated value and method attribute , But not satisfied headers attribute , The page shows 404 error , That is, the resource was not found
7、SpringMVC Support ant The path of style
?: Represents any single character
*: To express arbitrary 0 Characters or more
**: Represents any one or more levels of directories
Be careful : When use , Only use /**/xxx The way
8、SpringMVC Supports placeholders in paths ( a key )
Original way :/deleteUser?id=1
rest The way :/deleteUser/1
SpringMVC Placeholders in paths are often used for RESTful In style , When some data in the request path is transmitted to the server through the path , Can be in the corresponding @RequestMapping Annotated value Property through placeholders {xxx} Represents the transmitted data , Re pass **@PathVariable annotation **, Assign the data represented by the placeholder to the formal parameters of the controller method
<a th:href="@{/testRest/1/admin}"> Placeholder in test path -->/testRest</a><br>
@RequestMapping("/testRest/{id}/{username}")
public String testRest(@PathVariable("id") String id, @PathVariable("username") String username){
System.out.println("id:"+id+",username:"+username);
return "success";
}
边栏推荐
- Under the high debt of Red Star Macalline, is it eyeing new energy?
- Several methods of realizing high-low byte or high-low word exchange in TIA botu s7-1200
- 数据库连接数查看和修改
- Steam科学教育赋予课堂教学的创造力
- UE4 获取玩家控制权的两种方式
- 综合评价与决策方法
- Recommendation | scholar's art and Tao: writing papers is a skill
- MySQL usage
- Creative design principle of youth maker Education
- MySQL only checks the reasons for the slow execution of one line statements
猜你喜欢

Support proxy direct connection to Oracle database, jumpserver fortress v2.24.0 release

Keil v5安装和使用

Credit card fraud detection based on machine learning

Threadpooltaskexecutor and ThreadPoolExecutor

建设面向青少年的创客教育实验室

Several methods of realizing high-low byte or high-low word exchange in TIA botu s7-1200

Postman imports curl, exports curl, and exports corresponding language codes

What are the consequences and problems of computer system restoration

The auxiliary role of rational cognitive educational robot in teaching and entertainment

数据仓库
随机推荐
Segger embedded studio cannot find xxx.c or xxx.h file
Weights & Biases (二)
十一、异常处理器
dijikstra(先预处理)+dfs,relocation truncated to fit
ROS2的launch有何不同?
What are the duplicate check rules for English papers?
Compiled by egg serialize TS
Acwing_12. 背包问题求具体方案_dp
MySQL usage
ASP. Net core actionfilter filter details
远坂凛壁纸
吴恩达机器学习课后习题——逻辑回归
Functions of anonymous functions
Recommendation Book Educational Psychology: a book for tomorrow's teachers~
HelloWorld case analysis
Which websites can I visit to check the latest medical literature?
How to choose the key words of the thesis?
How to write the summary? (including 7 easily ignored precautions and a summary of common sentence patterns in 80% of review articles)
力扣每日一题-第42天-661. 图片平滑器
AWS Support Plan