@RequestMapping与@RequestParam注解

业无高卑志当坚,男儿有求安得闲?这篇文章主要讲述@RequestMapping与@RequestParam注解相关的知识,希望能为你提供帮助。
@RequestMapping注解
@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 = "https://www.songbingjia.com/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 = https://www.songbingjia.com/android/{"","/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 = "https://www.songbingjia.com/android/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来进行测试:
@RequestMapping与@RequestParam注解

文章图片

@RequestMapping与@RequestParam注解

文章图片

控制台打印结果:
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 = "https://www.songbingjia.com/android/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进行测试:
@RequestMapping与@RequestParam注解

文章图片

@RequestMapping与@RequestParam注解

文章图片

【@RequestMapping与@RequestParam注解】控制台打印结果:
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 = "https://www.songbingjia.com/android/get.do", method = RequestMethod.GET) public void get() { System.out.println("get"); }@RequestMapping(value = "https://www.songbingjia.com/android/post.do", method = RequestMethod.POST) public void post() { System.out.println("post"); }@RequestMapping(value = "https://www.songbingjia.com/android/delete.do", method = RequestMethod.DELETE) public void delete() { System.out.println("delete"); }@RequestMapping(value = "https://www.songbingjia.com/android/put.do", method = RequestMethod.PUT) public void put() { System.out.println("put"); }@RequestMapping(value = "https://www.songbingjia.com/android/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 = "https://www.songbingjia.com/android/test.do", params = "user=test") public void user(String user) { System.out.println(user); }// 当user参数为test以及alias参数为test时执行这个方法 @RequestMapping(value = "https://www.songbingjia.com/android/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 = "https://www.songbingjia.com/android/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 = "https://www.songbingjia.com/android/nothing") String name) { System.out.println(name); }@RequestMapping("user.do") // url上没有username以及alias参数时,给它设置一个默认值为nothing public void userAndAlias(@RequestParam(name = "username", defaultValue = "https://www.songbingjia.com/android/nothing") String user, @RequestParam(name = "alias", defaultValue = "https://www.songbingjia.com/android/nothing") String a) { System.out.println(user); System.out.println(a); } }


    推荐阅读