第一步添加jsckson的包
<dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.8</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.8</version>
第二步,在spring-mvc中添加(修改)如下配置:
!-- 設置對字符串的消息轉換器 --> <bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/plain;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> <!-- 避免IE執行AJAX時,返回JSON出現下載文件 --> <!-- Jackson 是Spring 自帶的功能 --> <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>application/json;charset=UTF-8</value> <value>application/xml;charset=UTF-8</value> <value>text/html;charset=UTF-8</value> <value>text/plain;charset=UTF-8</value> <value>text/xml;charset=UTF-8</value> <value>text/javascript;charset=UTF-8</value> </list> </property> </bean>
修改springmvc的注解如下:
<!-- 開啟對SpringMVC注解的支持 --> <mvc:annotation-driven conversion-service="conversionService2"> <!-- 配置以上兩個消息消息轉換器解決返回的Json數據中文亂碼問題--> <mvc:message-converters> <ref bean="stringHttpMessageConverter"/> <ref bean="mappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven>
基本配置完成,然后可以用來解析。。。后續操作