@RequestBody将json串转换为java对象
@ResponseBody将java对象转换为json串输出
都输出json数据,方便对结果解析
环境准备
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.6</version>
</dependency>
在适配器中加入messageConverters
<!--注解适配器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</list>
</property>
</bean>
如果使用<mvc:annotation-driven/>
则不用定义上边的内容
输入json
使用jquery的ajax进行提交json串
contentType:"application/j son;charset=utf-8"
//请求json串(商品信息),输出json(商品信息)
//@RequestBody将请求商品的json串转成对象
//@ResponseBody将对象转换成json串
@RequestMapping("/xxx")
publlic @ResponseBody Items requetJson(@RequestBody Items items){
return items;
}