日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

解决DeferredResult 使用 @ResponseBody 注解返回中文乱码

發布時間:2024/9/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 解决DeferredResult 使用 @ResponseBody 注解返回中文乱码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring MVC 項目在使用?DeferredResult 實現異步接口,但返回中文亂碼,顯示的都是問號。

相關代碼:

/*** 長輪詢* * @return*/@RequestMapping(value = "ctrldf.do", method = RequestMethod.GET)@ResponseBodypublic DeferredResult<Map<String, String>> DeferredControl(HttpServletRequest request,HttpServletResponse response) throws Exception {// 設置響應內容編碼,解決直接在瀏覽器地址欄訪問中文內容亂碼的問題response.setCharacterEncoding("utf-8");// 設置響應內容類型response.setContentType("application/json");DeferredResult<Map<String, String>> result = new DeferredResult<Map<String, String>>(10000l, null); // 設置超時10s,超時返回nullPersonal personal = SecurityUtils.getPersonal(request);Long personalId = personal.getId();if (personalId == null) {// outFailureJson(response, BaseCodeMessage.personal_10001);result.setErrorResult(BaseCodeMessage.personal_10001);return result;}Long familyId = personal.getFamilyId();if (familyId == null) {// outFailureJson(response, "1", "家庭編號不能為空");result.setErrorResult("家庭編號不能為空");return result;} }

其中嘗試使用

// 設置響應內容編碼,解決直接在瀏覽器地址欄訪問中文內容亂碼的問題response.setCharacterEncoding("utf-8");// 設置響應內容類型response.setContentType("application/json");發現根本不起作用。

解決方法:

需要在 spring-mvc.xml 中添加以下內容:

<!--避免IE執行AJAX時,返回JSON出現下載文件 --><bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/plain;charset=UTF-8</value><value>text/html;charset=UTF-8</value></list></property></bean><beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"><property name="messageConverters"><list><ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON轉換器 --></list></property></bean><mvc:annotation-driven />最初我是只添加了:

<!-- 啟動SpringMVC的注解功能,完成請求和注解POJO的映射 --><beanclass="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"><property name="messageConverters"><list><ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON轉換器 --></list></property></bean>這樣子使用 以下代碼 是沒有亂碼的。

相關代碼如下:

@RequestMapping("/savegw.do")@ResponseBodypublic void savegw(HttpServletRequest request, HttpServletResponse response) throws Exception { // 設置響應內容編碼,解決直接在瀏覽器地址欄訪問中文內容亂碼的問題response.setCharacterEncoding("utf-8");// 設置響應內容類型response.setContentType("text/plain");try {OutputStream out = response.getOutputStream();if (content == null){content = "";}out.write(content.getBytes());out.close();} catch (IOException e) {e.printStackTrace();} }有什么區別呢?

上面的方法返回值是?DeferredResult, 而這里返回值是 void。

另外看到有的文章中提到:

<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/plain;charset=utf-8</value><value>text/html;charset=UTF-8</value></list></property></bean>不知是用于什么情況。

注意:有文章提到 上面配置的 xml 必須在?<mvc:annotation-driven /> 之上,這個

總結

以上是生活随笔為你收集整理的解决DeferredResult 使用 @ResponseBody 注解返回中文乱码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。