zoukankan      html  css  js  c++  java
  • SpringMVC JSON乱码解决

    spring.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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--开启注解-->
    <context:component-scan base-package="com.rzk.controller"/>
    <!-- &lt;!&ndash;替代了 处理器适配器和映射器&ndash;&gt;-->
    <!-- <mvc:annotation-driven/>-->
    <!-- &lt;!&ndash;过滤静态资源&ndash;&gt;-->
    <!-- <mvc:default-servlet-handler/>-->

    <!--JSON乱码问题配置-->
    <mvc:annotation-driven>
    <mvc:message-converters>
    <bean class="org.springframework.http.converter.StringHttpMessageConverter">
    <constructor-arg value="UTF-8"/>
    </bean>
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
    <property name="objectMapper">
    <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
    <property name="failOnEmptyBeans" value="false"/>
    </bean>
    </property>
    </bean>
    </mvc:message-converters>
    </mvc:annotation-driven>



    <!--视图解析器 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"
    id="InternalResourceViewResolver">
    <!--前缀-->
    <property name="prefix" value="/WEB-INF/jsp/"/>
    <!--后缀-->
    <property name="suffix" value=".jsp"/>
    </bean>

    </beans>
    @Controller
    public class UserController {
        @RequestMapping(value = "/j1",produces = "application/json;charset=utf-8")
        @ResponseBody  //加上它,它就不会走视图解析器,会直接返回一个字符串
        public String JsonDome(){
            //创建一个对象
            User user = new User("",1,"嘿嘿");
            System.out.println(user);
            return user.toString();
        }
    
    }
    web.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
             version="4.0">
        <servlet>
            <servlet-name>springmvc</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>classpath:springmvc-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>springmvc</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
        <filter>
            <filter-name>encoding</filter-name>
            <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
            <init-param>
                <param-name>encoding</param-name>
                <param-value>utf-8</param-value>
            </init-param>
        </filter>
        <filter-mapping>
            <filter-name>encoding</filter-name>
            <url-pattern>/*</url-pattern>
        </filter-mapping>
    </web-app>

    导入maven依赖

    1         <dependency>
    2             <groupId>com.fasterxml.jackson.core</groupId>
    3             <artifactId>jackson-databind</artifactId>
    4             <version>2.10.0</version>
    5         </dependency>
  • 相关阅读:
    Java Socket通信读取相关信息代码
    Java Socket编程如何建立两者关系
    浅谈JAVA中如何利用socket进行网络编程(二)
    浅谈JAVA中如何利用socket进行网络编程(一)
    【Java TCP/IP Socket】TCP Socket(含代码)
    HTTP协议
    HTTP协议详解
    TCP/IP协议与Http协议的区别
    MultipartResolver实现文件上传功能
    ***CodeIgnite/CI 去掉 index.php的 配置
  • 原文地址:https://www.cnblogs.com/rzkwz/p/12681367.html
Copyright © 2011-2022 走看看