SSM架构 (Spring 5.0.2)添加Jackson

第一步添加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>

基本配置完成,然后可以用来解析。。。后续操作

上一篇:C#关闭一个特定的窗体(关闭特定的消息框或者普通窗体)


下一篇:html声明charset="utf-8"后,浏览器访问中文依旧乱码(绝对有效)