响应JSON数据【jackjson】
添加依赖:
对于Gson和jackson这两个json处理依赖,直接添加即可。
除此之外,其他的json解析器如fastjson都需要手动配置HttpMessageConverter.
实际上,在SpringMVC中,是由一个名叫HttpMessageConverter的类
来提供对象到JSON字符串的转换的。
而SpringMVC默认就提供了Gson和Jackson的HttpMessageConverter,
分别是org.springframework.http.converter.json.GsonHttpMessageConverter
和MappingJackson2HttpMessageConverter。
对于其他的JSON解析器,只需要开发者手动配置一下HttpMessageConverter即可。
本案例使用 jackson处理
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!-- 开启扫描 -->
<context:component-scan base-package="com.sxt.*"/>
<!-- 开启SpringMVC注解的方式 -->
<mvc:annotation-driven></mvc:annotation-driven>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringMVC-01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.