springmvc学习笔记(18)-json数据交互
标签: springmvc
本文主要介绍如何在springmvc中进行json数据的交互,先是环境准备和配置,然后分别展示了“输入json串,输出是json串”和“输入key/value,输出是json串”两种情况下的交互
springmvc进行json交互json数据格式在接口调用中、html页面中较常用,json格式比较简单,解析还比较方便。
比如:webservice接口,传输json数据.
最开始我少了jackson-databind依赖,程序各种报错。
<span class="hljs-comment"><!-- json 转换--></span> <span class="hljs-tag"><<span class="hljs-title">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>com.fasterxml.jackson.core<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span> <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>jackson-databind<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span> <span class="hljs-tag"><<span class="hljs-title">version</span>></span>2.7.2<span class="hljs-tag"></<span class="hljs-title">version</span>></span> <span class="hljs-tag"></<span class="hljs-title">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-title">dependency</span>></span> <span class="hljs-tag"><<span class="hljs-title">groupId</span>></span>org.codehaus.jackson<span class="hljs-tag"></<span class="hljs-title">groupId</span>></span> <span class="hljs-tag"><<span class="hljs-title">artifactId</span>></span>jackson-mapper-asl<span class="hljs-tag"></<span class="hljs-title">artifactId</span>></span> <span class="hljs-tag"><<span class="hljs-title">version</span>></span>1.9.13<span class="hljs-tag"></<span class="hljs-title">version</span>></span> <span class="hljs-tag"></<span class="hljs-title">dependency</span>></span>查看依赖树
[INFO] +- <span class="hljs-keyword">com</span><span class="hljs-preprocessor">.fasterxml</span><span class="hljs-preprocessor">.jackson</span><span class="hljs-preprocessor">.core</span>:jackson-databind:jar:<span class="hljs-number">2.7</span><span class="hljs-number">.2</span>:compile [INFO] | +- <span class="hljs-keyword">com</span><span class="hljs-preprocessor">.fasterxml</span><span class="hljs-preprocessor">.jackson</span><span class="hljs-preprocessor">.core</span>:jackson-annotations:jar:<span class="hljs-number">2.7</span><span class="hljs-number">.0</span>:compile [INFO] | /- <span class="hljs-keyword">com</span><span class="hljs-preprocessor">.fasterxml</span><span class="hljs-preprocessor">.jackson</span><span class="hljs-preprocessor">.core</span>:jackson-core:jar:<span class="hljs-number">2.7</span><span class="hljs-number">.2</span>:compile [INFO] /- org<span class="hljs-preprocessor">.codehaus</span><span class="hljs-preprocessor">.jackson</span>:jackson-mapper-asl:jar:<span class="hljs-number">1.9</span><span class="hljs-number">.13</span>:compile [INFO] /- org<span class="hljs-preprocessor">.codehaus</span><span class="hljs-preprocessor">.jackson</span>:jackson-core-asl:jar:<span class="hljs-number">1.9</span><span class="hljs-number">.13</span>:compile 配置json转换器在注解适配器中加入messageConverters
<span class="hljs-comment"><!--注解适配器 --></span> <span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"</span>></span> <span class="hljs-tag"><<span class="hljs-title">property</span> <span class="hljs-attribute">name</span>=<span class="hljs-value">"messageConverters"</span>></span> <span class="hljs-tag"><<span class="hljs-title">list</span>></span> <span class="hljs-tag"><<span class="hljs-title">bean</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">"org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"</span>></span><span class="hljs-tag"></<span class="hljs-title">bean</span>></span> <span class="hljs-tag"></<span class="hljs-title">list</span>></span> <span class="hljs-tag"></<span class="hljs-title">property</span>></span> <span class="hljs-tag"></<span class="hljs-title">bean</span>></span>注意:如果使用<mvc:annotation-driven />则不用定义上边的内容。
json交互测试