springmvc返回中文亂碼解決
阿新 • • 發佈:2019-02-03
解決方案一:
在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.jackson2.9.5(當前最新版本20180417)不相容spring5,所以這種方法得使用spring4,不相容的話就會報java.lang.NoClassDefFoundError: com/fasterxml/jackson/annotation/JsonMerge之類的異常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>