springMVC请求注解@RequestMapping各个属性值
最近遇到了一个采用fastJson传输数据的方式,搞了半天,总是感觉模糊,觉得自己有必要在这里做一个系统的总结,今天先从@RequestMapping的属性开始,采用REST 风格的 URL 请求,REST(Representational State Transfer):(资源)表现层状态转化,它是目前最流行的一种软件架构,其结构清晰、易于理解、扩展方便且符合标准。
1、RequestMapping接口的源码如下,里面定义了七个属性
public interface RequestMapping extends Annotation {
// 指定映射的名称
public abstract String name();
// 指定请求路径的地址
public abstract String[] value();
// 指定请求的方式,是一个RequsetMethod数组,可以配置多个方法
public abstract RequestMethod[] method();
// 指定参数的类型
public abstract String[] params();
public abstract String[] headers();
// 指定数据请求的格式
public abstract String[] consumes();
// 指定返回的内容类型
public abstract String[] produces();
}
2、下面是我在服务器端编写的一个程序,我将@RequestMapping中的参数都采用了符合原接口的定义的数组的数据类型。
@RequestMapping(value = {"/modifyGet.do","/modifyGet1.do"}, method={RequestMethod.POST, RequestMethod.GET},
consumes={"application/json"}, produces={"application/json"}, params={"name=mike","pwd=123456"},headers={"a=1"})
@ResponseBody
public Object addEmpGet()throws Exception {
JSONObject responseObj = new JSONObject();
responseObj.put("id", 1/*reqObj.getIntValue("id")*/);
return responseObj ;
}
2.1 测试value属性:映射的是请求的地址,如果是一个数据,那么这两个地址之间的关系是或(||)的关系,请看下面是springmvc启动之后的提示:
2017-08-20 20:04:11,307 INFO [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping] -
Mapped "{[/modifyGet.do || /modifyGet1.do],methods=[POST || GET],params=[name=mike && pwd=123456],headers=[a=1],
consumes=[application/json],produces=[application/json],produces=[],custom=[]}" onto public java.lang.Object springMVCmybatis.modifyController.addEmpGet()
throws java.lang.
不仅如此,采用postMan发送请求之后,处理的结果也是一样的。
postMan发送请求的地址:
http://localhost:8080/springMVCmybatis/modifyGet1.do?name=mike&pwd=123456
http://localhost:8080/springMVCmybatis/modifyGet.do?name=mike&pwd=123456
postMan接收到的结果:
{"id":1}
2.2、method属性:定义了请求的方式,如果此属性定义了多个值,那么可以适应多种请求方式,比如此例中我定义了RequestMethod.POST与RequestMethod.GET两种方法,采用postMan发送两种请求也可以得到正确的JSON数据。正确的请求方式如下:

问题:如果将服务器端代码改成如下格式,那么发送的请求是GET就会报错了,POST请求就不会报错,代码如下
@RequestMapping(value = {"/modifyGet.do","/modifyGet1.do"}, method={RequestMethod.POST, RequestMethod.GET},
consumes={"application/json"}, params={"name=mike","pwd=123456"},headers="a=1")
@ResponseBody
public Object addEmpGet(@RequestBody JSONObject reqobj)throws Exception {
JSONObject responseObj = new JSONObject();
responseObj.put("id", reqObj.getIntValue("id"));
return responseObj ;
}
2.3、consumes属性 :指定处理请求的提交内容类型(Content-Type),例如application/json, text/html;
produces属性:指定返回的内容类型,仅当request请求头中的(Accept)类型中包含该指定类型才返回,本人实验了一下,如果请求的头中不包含这个属性也能有正确的返回值,但是如果有Accept这个属性,那么前后要匹配,否则输出报错;
2.4、params与heads:这两个属性的作用是类似的,可以对请求进一步过滤,如果输入的参数不包含对应的属性或者属性的值有错误,那么就会报HTTP Status [404] – [Not Found]的错误。
参考博客:
1、https://my.oschina.net/kolbe/blog/509810
2、http://blog.csdn.net/walkerjong/article/details/7994326
springMVC请求注解@RequestMapping各个属性值的更多相关文章
- SpringMVC使用注解@RequestMapping映射请求
pringMVC通过使用@RequestMapping注解,实现指定控制器可以处理哪些URL请求. 控制器的类定义及方法定义处都可以标注@RequestMapping: 类定义处:提供初步的请求映射信 ...
- 【SpringMVC】SpringMVC系列5之@RequestHeader 映射请求头属性值
5.@RequestHeader 映射请求头属性值 5.1.概述 请求头包含了若干个属性,服务器可据此获知客户端的信息,通过 @RequestHeader 即可将请求头中的属性值绑定到处理方法的入参中 ...
- SpringMVC请求参数注解两个小问题
今天遇到使用SpringMVC请求注解遇到的两个小问题: 如果用@requestBody注解,则请求体内容类型一般要为application/json,如果其类型为multipart/form-dat ...
- springBoot注解大全JPA注解springMVC相关注解全局异常处理
https://www.cnblogs.com/tanwei81/p/6814022.html 一.注解(annotations)列表 @SpringBootApplication:包含了@Compo ...
- springmvc(3)注解
有疑问可以参考博主其他关于spring mvc的博文 此时直接进行代码的实现 一般的步骤: -加入jar包 -配置DispatcherServlet -加入Spring MVC配置文件 -编写请求的处 ...
- SpringMVC @ModelAttribute注解
/** * 1. 有 @ModelAttribute 标记的方法, 会在每个目标方法执行之前被 SpringMVC 调用! * 2. @ModelAttribute 注解也可以来修饰 ...
- 外部配置属性值是如何被绑定到XxxProperties类属性上的?--SpringBoot源码(五)
注:该源码分析对应SpringBoot版本为2.1.0.RELEASE 1 前言 本篇接 SpringBoot是如何实现自动配置的?--SpringBoot源码(四) 温故而知新,我们来简单回顾一下上 ...
- SpringMVC(六):@RequestMapping下使用@RequestHeader绑定请求报头的属性值、@CookieValue绑定请求中的Cookie值
备注:我本地浏览器的报头(Request Header)信息如下: Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image ...
- 前台jquery+ajax+json传值,后台处理完后返回json字符串,如何取里面的属性值?(不用springmvc注解)
一.取属性值 前台页面: function select(id){ alert("hfdfhdfh"+id); $.ajax({ url:"selectByid.jsp& ...
随机推荐
- lammps模拟化学反应(1)
1. Can I use lammps to chemical reaction systems?Please note that you can only get as good an answer ...
- [JAVA][StringUtils]字符串工具类的常用方
StringUtils 方法的操作对象是 java.lang.String 类型的对象,是 JDK 提供的 String 类型操作方法的补充,并且是 null 安全的(即如果输入参数 String 为 ...
- YUI前端优化之Server篇
二.网站Server 篇:使用内容分发网络为文件头指定Expires或Cache-ControlGzip压缩文件内容配置ETag尽早刷新输出缓冲使用GET来完成AJAX请求 11.使用内容分发网络 用 ...
- MVC三者关系
- iOS6后的内存警告处理
[iOS6后的内存警告处理] The memory used by a view to draw itself onscreen is potentially quite large. However ...
- Google Tango Java SDK开发:Configure and Connect 配置和连接
Configure and Connect 配置和连接 Note: This section assumes you are familiar with the Android Activity Li ...
- [Selenium With C#基础教程] Lesson-03 超级链接
作者:Surpassme 来源:http://www.jianshu.com/p/83809943e751 声明:本文为原创文章,如需转载请在文章页面明显位置给出原文链接,谢谢. 超级链接或链接是We ...
- HttpClient Timeout
1. Overview This tutorial will show how to configure a timeout with the Apache HttpClient 4. If you ...
- 18个扩展让你的Firefox成为渗透测试工具
Firefox是一个出自Mozilla组织的流行的web浏览器.Firefox的流行并不仅仅是因为它是一个好的浏览器,而是因为它能够支持插件进而加强它自身的功能.Mozilla有一个插件站点,在那里面 ...
- 怎样运用servlet
制作登陆界面 login.html <!DOCTYPE html> <html> <head> <title>login.html</title& ...