1. 程式人生 > >springmvc返回中文亂碼解決

springmvc返回中文亂碼解決

解決方案一:

在spring配置中增加以下配置:

<mvc:annotation-driven>
       <mvc:message-converters register-defaults="true">
              <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                     <constructor-arg value="UTF-8" />
              </bean>
</mvc:message-converters> </mvc:annotation-driven>

然後增加以下依賴:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.5</version>
</dependency>
<dependency>
    <groupId>
com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.5</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>
2.9.5</version> </dependency>
注意:com.fasterxml.jackson2.9.5(當前最新版本20180417)不相容spring5,所以這種方法得使用spring4,不相容的話就會報java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonMerge之類的異常