標籤:eth ram pat ati isp map col [] content
1、@RequestMapping可以註解在類上和方法上,當註解在類傷的時候,所有的方法都將映射為相對於類層級的請求
如下代碼
1 @RequestMapping(value="/user")2 public class UserController{3 // 該方法映射的請求為http://localhost:8080/user/login4 @RequestMapping("/login")5 public String login() {6 return "login";7 }8 }View Code
2、@RequestMapping註解說明
| 屬性 |
類型 |
是否必要 |
說明 |
| value |
String[] |
否 |
用於將制定請求的實際地址映射到方法上 |
| name |
String |
否 |
給映射地址制定一個別名 |
| method |
RequestMethod[] |
否 |
映射指定請求的方法的類型,包括GET,POST,HEAD,OPTIONS,PUT,PATCH,DELETE,TRACE |
| consumes |
String[] |
否 |
指定處理請求提交的內容類型(Content-Type),例如application/json,text/html等 |
| params |
String[] |
否 |
指定request中必須包含某些參數值時,才讓該方法進行處理 |
| produces |
String[] |
否 |
指定返回的內容類型,返回的內容類型必須是request要求標頭(Accept)中包含的類型 |
| headers |
String[] |
否 |
指定request中必須包含某些指定的header值時,才讓該方法進行處理 |
1 @RequestMapping(value="/register")2 public String registerForm() {3 4 return "registerForm";5 }View Code
URL訪問方式如下:http://localhost:8080/user/register
@RequestMapping(value="/register",method=RequestMethod.POST) 同時也支援多個http的請求方式 @RequestMapping(value="/register",method={RequestMethod.POST,RequestMethod.GET})
@RequestMapping(value="/register",method=RequestMethod.POST,consumes="application/json")該方法標識僅畜類 request Content-Type為“application/json”類型的請求
@RequestMapping(value="/register",method=RequestMethod.POST,produces="application/json")該方法標識僅處理 request 要求標頭Accept頭中包含了“application/json”類型的請求,同時也指明了返回的內容的類型為application/json
@RequestMapping註解