`
a137268431
  • 浏览: 156804 次
文章分类
社区版块
存档分类
最新评论

spring MVC 中json格式数据发送与接收

 
阅读更多
在web程序开发中,前端和服务器数据的传输方式常用的有两种,一种是以表单的形式提交,此时可以利用jquery的serialize()方法将表单内容转为a=1&b=2&c=3&d=4&e=5这样的格式传输过去,接收端则可以用javabean直接接收。
还有一种方式是以json格式传输,接收时若直接用bean接收则接收不到,此时应该用@RequestBody方式,需要注意的是接收的需要是json串,而不是json对象,可以在发送前使用JSON.stringify函数进行处理。
例子如下
客户端:
<wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">var url=path+'testConverter.html';</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">$.ajax( {</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">url : url,</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">type : "POST",</span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">dataType:"json",</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">contentType:'application/json;charset=UTF-8</span><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">',</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">data:JSON.stringify</span><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">({userId:'1',userName:'hello',password:'test',credits:'2',lastIp:'',lastVisit:'1986-05-27'}),</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">success : function(data) {</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">alert(data.userName);</span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">},</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">error:function(e){</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">alert("err");</span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">}</span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">服务端:</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">@RequestMapping(value="/testConverter.html")</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">@ResponseBody</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">public User testConverter(@RequestBody User</span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">user)</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">{</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">System.out.println(user.getUserName());</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">user.setUserName("testname");</span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">return user;</span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"></span><wbr style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">}</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">上面例子中服务端返回的是个对象,@ResponseBody函数会自动将其转换为客户端要求的‘ dataType:"json",’格式。</span><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><br style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)"><span style="color:rgb(68,68,68); font-family:simsun; font-size:14.399999618530273px; line-height:21.600000381469727px; background-color:rgb(243,248,251)">最后需要注意的是在xxx-serverlet配置文件中应该写上,以便可以使用 @ResponseBody和@RequestBody</span> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
分享到:
评论

相关推荐

    spring mvc(整合了json)

    2. **配置 Spring MVC**:在 Spring MVC 的配置文件中,我们需要添加 `Jackson` 的转换器,使得 Spring MVC 能够解析和生成 JSON 数据。例如,在使用 XML 配置时,可以添加以下配置: ```xml &lt;bean class="org....

    Spring MVC返回JSON数据 用到的jar包

    在Spring MVC中,为了将Java对象转换成JSON格式并返回给客户端,我们需要依赖于JSON库。在提供的信息中,提到了`jackson-all-1.9.x.jar`系列的包,这是Jackson库的一个早期版本。Jackson是一个非常流行的JSON处理库...

    spring-mvc-jsonview源代码

    它不仅提供了丰富的功能,还能够与各种技术进行集成,例如支持JSON数据格式的处理,使得前后端交互更加高效。本篇文章将通过解析“spring-mvc-jsonview”源代码,深入探讨Spring MVC如何支持AJAX请求,并提供一个...

    spring4 mvc json配置jar包

    在开发基于Spring4 MVC的Web应用时,JSON(JavaScript Object Notation)是一...通过正确配置和使用这些库,开发者可以轻松地将Java对象转换为JSON格式,发送到客户端,或者接收JSON数据并转换为Java对象进行业务处理。

    Spring mvc Json处理实现流程代码实例

    当前端(如浏览器)向服务器发送JSON数据时,Spring MVC 提供了多种接收方式。一种是通过`HttpServletRequest`的`getInputStream()`方法读取原始输入流,然后使用如Jackson库的`ObjectMapper`解析JSON数据到Java...

    springmvc操作json数据所需的jar包

    在Spring MVC框架中,处理JSON数据是Web应用开发中的常见任务。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,它使得前后端数据交互变得更加简单和直观。本篇文章将详细讲解在Spring MVC中如何...

    spring mvc json学习

    在Spring MVC中,JSON数据通常用于客户端与服务器之间的数据传输,比如AJAX请求。为了将Java对象转换为JSON,我们需要引入Jackson库,这是一个流行的JSON处理库。在项目中添加Jackson依赖后,Spring MVC会自动配置并...

    SpringMVC中后台转换json格式

    在前后端交互中,JSON(JavaScript Object Notation)是一种常用的数据交换格式,它轻量级且易于人阅读和编写,同时也方便机器解析和生成。然而,当我们在使用SpringMVC和Ajax进行数据交互时,可能会遇到406错误,这...

    spring mvc拦截器过滤json中的null值

    在Spring MVC框架中,我们经常需要处理JSON数据,这些数据可能包含null值。在某些情况下,为了保持数据的整洁和避免后端处理null值时出现异常,我们需要在数据传输前过滤掉这些null值。本教程将详细介绍如何使用...

    Spring MVC不能直接接收list类型参数的问题

    如果采用 POST 方法传输数据,并且希望以 JSON 格式发送数据,则可以使用 `@RequestBody` 注解。 **前端 AJAX 请求示例**: ```javascript $.ajax({ url: "xxxx", type: 'POST', dataType: "json", contentType:...

    spring—mvc -json

    此外,Spring MVC还支持使用`@RequestBody`注解接收JSON数据,以及使用`@ModelAttribute`与表单数据绑定。在实际开发中,还可以利用Spring Data REST自动创建RESTful服务,或者使用`@JsonInclude`和`@JsonView`等...

    Spring MVC跟jQuery和Twitter Bootstrap的结合

    3. **数据绑定**:Spring MVC支持JSON或XML格式的数据交换,jQuery可以通过Ajax请求发送和接收这些格式的数据。使用`@RequestBody`和`@ResponseBody`注解,Spring MVC可以将JSON数据自动映射到Java对象。 4. **表单...

    Spring mvc 接收json对象

    在Spring MVC框架中,接收JSON格式的数据是一种常见的需求,尤其是在实现RESTful Web服务的时候。Spring MVC通过注解和HTTP消息转换器简化了这一过程。本文主要介绍如何在Spring MVC中接收JSON数据对象,并通过示例...

    最全最经典spring-mvc教程

    另外,Spring MVC与Spring框架的其他组件无缝集成,如Spring AOP(面向切面编程)用于实现日志、事务管理等功能,Spring JDBC和MyBatis等持久层框架用于数据库操作,以及Spring Data JPA、Hibernate等ORM工具,使得...

    spring-mvc-官方中文文档

    12. **消息转换**:Spring MVC 提供了 HttpMessageConverter 接口,用于将 HTTP 请求和响应体与 Java 对象之间进行转换,支持 JSON、XML 等格式。 13. **RESTful 风格**:Spring MVC 可以很方便地支持 RESTful 风格...

    springmvc json配置

    在Spring MVC框架中,JSON(JavaScript Object Notation)是一种常用的数据交换格式,用于在服务器与客户端之间传输数据。本文将详细介绍如何在Spring MVC项目中配置JSON支持,以及所需的依赖包。 1. **JSON简介** ...

    Spring MVC使用Demo

    在Spring MVC中,Controller处理用户的请求,Model存储数据,而View负责渲染结果。 创建Spring MVC项目的第一步通常是设置开发环境。这通常包括安装Java Development Kit (JDK)、配置IDE(如IntelliJ IDEA或Eclipse...

    Spring MVC 数据转换和格式化.docx

    总的来说,Spring MVC的数据转换和格式化机制通过HttpMessageConverter和Converter接口提供了强大的灵活性和可扩展性,使我们能够处理各种数据格式,满足不同应用场景的需求。无论是处理JSON数据,还是进行特定类型...

    springmvc对json支持

    4. **@RequestBody注解**:在接收客户端发送的JSON数据时,我们可以使用`@RequestBody`注解。这会将请求体中的JSON数据转换为Java对象,使得我们可以直接在方法参数中使用。 5. **类型转换**:Jackson库使用`@...

Global site tag (gtag.js) - Google Analytics