springMVC 返回中文字符串時亂碼

SpringMVC的@ResponseBody註解可以將請求方法返回的對象直接轉換成JSON對象,但是當返回值是String的時候,中文會亂碼

解決辦法

第一種:在@RequestMapping中添加produces=”text/html;charset=UTF-8

第二種:在MVC 配置中加入,spring版本必需爲3.1或以上版本纔可以下配置

<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>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章