@RequestMapping與@RequestParam註解
@RequestMapping 是 Spring Web 應用程序中最常被用到的註解之一。這個註解會將 HTTP 請求映射到 MVC 和 REST 控制器的處理方法上。 該註解不僅可以用於方法上,還可以用於類上,表示類中的所有響應請求的方法都是以該地址作為父路徑,例如可以加個虛擬目錄什麽的。
代碼示例:
package org.zero01.test; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/test") public class Test { @RequestMapping("/test.do") public void method(){ System.out.println("This is a test output"); } }
此時,需要訪問到該控制器中的method方法的話,就需要加上 /test 這個虛擬目錄,例如:
http://localhost:8090/test/test.do
接下來簡單介紹@RequestMapping註解中的屬性:
1.value與path屬性,這兩個屬性是一樣的,用於指定HTTP請求資源(URI)的實際映射地址,當不指定具體的屬性值時,默認是給該屬性傳遞值,示例:
package org.zero01.test; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/test") public class Test { @RequestMapping(value = "/value.do") public void value(){ System.out.println("value"); } @RequestMapping(path = "/path.do") public void path(){ System.out.println("path"); } @RequestMapping("/method.do") public void method(){ System.out.println("method"); } }
此時,可以訪問以下幾個地址:
http://localhost:8090/test/value.do
http://localhost:8090/test/path.do
http://localhost:8090/test/method.do
這兩個屬性都可以配置多個URI地址,示例:
package org.zero01.test; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller @RequestMapping("/test") public class Test { @RequestMapping(value = {"","/view.do","index*","/**/jsp.do","/value.do"}) public void value(){ System.out.println("value"); } }
2.consumes屬性,這個屬性指定處理何種請求的提交內容類型(Content-Type),例如application/json, text/html;等,示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class Test {
// 只處理Content-Type的值為application/json的請求
@RequestMapping(value = "json.do", consumes = "application/json")
public void json(){
System.out.println("application/json");
}
// 只處理Content-Type的值為text/html的請求
@RequestMapping(path = "/xml.do", consumes = "text/html")
public void xml() {
System.out.println("text/html");
}
}
我們可以通過Postman來進行測試:
控制臺打印結果:
application/json
text/html
3.produces屬性,該屬性用於指定返回的內容類型,僅當request請求頭中的(Accept)類型中包含該指定類型才返回,示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class Test {
// 該方法僅處理request請求中Accept頭中包含了"application/json"的請求,同時暗示了返回的內容類型為application/json;
@RequestMapping(value = "json.do", produces = "application/json")
public void json(){
System.out.println("application/json");
}
// 該方法僅處理request請求中Accept頭中包含了"text/xml"的請求,同時暗示了返回的內容類型為text/xml;
@RequestMapping(path = "/xml.do", produces = "text/html")
public void xml() {
System.out.println("text/html");
}
}
同樣可以使用Postman進行測試:
控制臺打印結果:
application/json
text/html
4.method屬性,從名稱就可以看出這個屬性用於指定請求的method類型, 例如GET、POST、PUT、DELETE等,示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/test")
public class Test {
@RequestMapping(value = "get.do", method = RequestMethod.GET)
public void get() {
System.out.println("get");
}
@RequestMapping(value = "post.do", method = RequestMethod.POST)
public void post() {
System.out.println("post");
}
@RequestMapping(value = "delete.do", method = RequestMethod.DELETE)
public void delete() {
System.out.println("delete");
}
@RequestMapping(value = "put.do", method = RequestMethod.PUT)
public void put() {
System.out.println("put");
}
@RequestMapping(value = "patch.do", method = RequestMethod.PATCH)
public void patch() {
System.out.println("patch");
}
}
5.params屬性,該屬性用於指定request裏某些參數為某個特定的值時,才讓該方法處理,示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class Test {
// 當user參數為test時執行這個方法
@RequestMapping(value = "test.do", params = "user=test")
public void user(String user) {
System.out.println(user);
}
// 當user參數為test以及alias參數為test時執行這個方法
@RequestMapping(value = "test.do", params = {"user=test","alias=test"})
public void userAndAlias(String user, String alias) {
System.out.println(user);
System.out.println(alias);
}
}
註:在SpringMVC中,會自動將request中的參數傳遞到對應的方法參數上。
6.headers屬性,該屬性指定request中必須包含某些指定的header值,才能讓該方法處理請求,示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/test")
public class Test {
@RequestMapping(value = "test.do", headers = "Referer=http://www.xxx.com/")
public void test() {
System.out.println("test");
}
}
@RequestParam註解
@RequestParam註解比較簡單,它用於將請求參數區數據映射到功能處理方法的參數上,自SpringMVC4.2之後,@RequestParam註解內部有4個參數:
- String name
- String value
- boolean required
- String defaultValue
1.name與value屬性,其中name和value分別是URL參數的名稱,即二者沒區別,我個人比較喜歡用name,因為在HTML的表單中我們都是用name屬性來設置URL參數名稱的,所以在註解上也使用name這個屬性的話,會更直觀一些。示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/test")
public class Test {
@RequestMapping("test.do")
// 指定將username參數的值傳遞到該方法的name參數上
public void test(@RequestParam(name = "username") String name) {
System.out.println(name);
}
@RequestMapping("user.do")
// 指定將username參數的值傳遞到該方法的user參數上,alias參數的值則傳遞到該方法的a參數上
public void userAndAlias(@RequestParam(name = "username")String user, @RequestParam(name = "alias")String a) {
System.out.println(user);
System.out.println(a);
}
}
2.required屬性,該屬性用於指定某個參數是否是必須的,默認值為true,表示請求中一定要有相應的參數,否則將報404錯誤碼,示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/test")
public class Test {
@RequestMapping("test.do")
// 指定username參數是必須的,如果url上沒有的話就會報錯
public void test(@RequestParam(name = "username", required = true) String name) {
System.out.println(name);
}
@RequestMapping("user.do")
// 指定username與alias參數不是必須的,即便url上沒有也不會報錯
public void userAndAlias(@RequestParam(name = "username", required = false) String user, @RequestParam(name = "alias", required = false) String a) {
System.out.println(user);
System.out.println(a);
}
}
3.defaultValue屬性,該屬性用於指定參數的默認值,表示如果請求中沒有同名參數時的默認值,默認值可以是SpEL表達式,如“#{systemProperties[‘java.vm.version‘]}”。示例:
package org.zero01.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@Controller
@RequestMapping("/test")
public class Test {
@RequestMapping("test.do")
// url上沒有username參數時,給它設置一個默認值為nothing
public void test(@RequestParam(name = "username", defaultValue = "nothing") String name) {
System.out.println(name);
}
@RequestMapping("user.do")
// url上沒有username以及alias參數時,給它設置一個默認值為nothing
public void userAndAlias(@RequestParam(name = "username", defaultValue = "nothing") String user, @RequestParam(name = "alias", defaultValue = "nothing") String a) {
System.out.println(user);
System.out.println(a);
}
}
@RequestMapping與@RequestParam註解