實際上是想用來做JSON的返回,團隊比較習(xí)慣用org.json。。。。
在項目中使用了@ResponseBody來注解一個返回值為String的函數(shù)。函數(shù)里邊獲取了一些鍵值對,鍵值對用org.json轉(zhuǎn)成了json格式,然而在輸出的時候出了問題。響應(yīng)結(jié)果中所有的中文都變成了問號,而在return前直接用System.out.println輸出到控制臺卻不是亂碼或者問號。在網(wǎng)上搜了一些解決方案,不管是加在RequestMapping里的content-type(完全看不到變化)還是加在xml的annotation-driven(直接編輯器保錯找不見這么個用法)。求教這個版本的SpringMVC應(yīng)該如何從根本上解決掉亂碼問題?
至于為啥不用直接返回Jackson的生成方式,額。。。。。。實在解決不了亂碼問題再考慮。。。
認(rèn)證高級PHP講師
Try adding startup parameters: -Dfile.encoding=utf-8
Based on your description, I guess it’s a problem with MessageConverter
Add this
<bean
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonMessageConverter" />
</list>
</property>
</bean>
<!-- Configure bean to convert JSON to POJO and vice versa -->
<bean id="jsonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
</bean>
It has been solved. Annotation-driven is not supported because the namespace definition file referenced by the xml header generated by idea itself is 3.0. Just change it to the latest one. In the end, the message-converter is reset through annotation-driven. Encoded properties.