SpringMvc处理json数据
2017-05-26 15:30
295 查看
SpringMvc提供了处理JSON格式请求响应的HttpMessageConverter接口,springmvc默认使用Jackson开源类包实现这个接口,即MappingJacksonHttpMessageConveter
注意
type:”POST”
contentType:”application/json” 表示请求的参数是json格式的
dataType:”json” 表示服务端返回的数据是json格式的
data:’{“id”:1}’ 请求的参数,双引号必须在里面
springmvc会解析json中传递的数据,将对应的属性设置到ItemsCustomer对象的相应的属性中
那么这个其中已经默认为我们配置好了读写Json功能,使用Jackson实现
使用原始的配置
如果在springweb的容器中显示定义了一个RequestMappingHandlerAdapter(像下面这样的),那么springMvc的RequestMappingHandlerAdapter默认装配的HttpMessageConverter将不再起作用,需要自己手动装配
为什么配置在处理器适配器中:
因为在执行handler之前是通过HandlerAdapter将请求的参数转换为相应的形参的属性值的,这里面涉及到json数据的转换,java类型之间的转换,数据的验证
在客户端以json格式的参数请求数据
在这里我么使用ajax请求<script type="text/javascript"> function jsonrequest(){ $.ajax({ type:"POST", url:"${pageContext.request.contextPath}/queryitemsUsejson.action", contentType:"application/json", /* 注意data的写法,不能是"{'id':1}" throws java.lang.Exception]: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Unexpected character (''' (code 39)): was expecting double-quote to start field name*/ data:'{"id":1}', dataType:"json", success:function(data){ alert(data.name+"\t"+data.id+"\t"+data.detail) }, error:function(){ alert("未知错误") } }) } </script>
注意
type:”POST”
contentType:”application/json” 表示请求的参数是json格式的
dataType:”json” 表示服务端返回的数据是json格式的
data:’{“id”:1}’ 请求的参数,双引号必须在里面
服务端处理请求
@RequestMapping("/queryitemsUsejson") public @ResponseBody ItemsCustomer queryitemsUsejson(@RequestBody ItemsCustomer ic) throws Exception{ ItemsCustomer itemsCustomer = itemsService.findItemsById(ic.getId()); return itemsCustomer; }
springmvc会解析json中传递的数据,将对应的属性设置到ItemsCustomer对象的相应的属性中
配置
如果使用了下面的这种简略的配置方式:<mvc:annotation-driven conversion-service="conversionService" />
那么这个其中已经默认为我们配置好了读写Json功能,使用Jackson实现
使用原始的配置
如果在springweb的容器中显示定义了一个RequestMappingHandlerAdapter(像下面这样的),那么springMvc的RequestMappingHandlerAdapter默认装配的HttpMessageConverter将不再起作用,需要自己手动装配
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <!-- 配置json解析器 --> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean> </list> </property> </bean>
为什么配置在处理器适配器中:
因为在执行handler之前是通过HandlerAdapter将请求的参数转换为相应的形参的属性值的,这里面涉及到json数据的转换,java类型之间的转换,数据的验证
相关文章推荐
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- SpringMVC处理Json数据
- SpringMVC-9 处理JSON数据
- SpringMVC,接受、处理Json,fastjson解析json,用Postman4发送json数据
- Springmvc处理JSON数据(七)
- springmvc接收和处理json数据
- springmvc处理json数据
- SpringMVC如何处理JSON数据和运行流程
- SpringMVC中处理json数据
- SpringMVC-处理POST提交JSON数据
- SpringMVC返回JSON数据406错误处理
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- springmvc 使用fastjson 处理 json 数据时中文乱码
- SpringMVC 返回Java8 时间JSON数据的格式化问题处理
- springmvc 4.x 处理json 数据时中文乱码
- SpringMVC处理Ajax请求(请求数据为 json 格式字符串)
- SpringMVC实战(五)-处理POST提交JSON数据
- SpringMVC中出现" 400 Bad Request "错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法
- (九)springmvc之json的处理(服务端发送json数据到客户端)
- JAVAWEB开发之SpringMVC详解(二)——高级开发、数据回显、参数绑定集合、图片上传、json交互、validation校验、异常处理、RESTful支持、拦截器