Spring MVC 關(guān)于controller的字符編碼問題
在使用springMVC框架構(gòu)建web應(yīng)用,客戶端常會(huì)請求字符串、整型、json等格式的數(shù)據(jù),通常使用@ResponseBody注解使 controller回應(yīng)相應(yīng)的數(shù)據(jù)而不是去渲染某個(gè)頁面。如果請求的是非英文格式的字符串,往往在客戶端顯示的是亂碼。原因是spring的 StringHttpMessageConverter默認(rèn)的字符類型是iso8895-1 ‘西歐語言’,中文等字符需要單獨(dú)指定。
這里總結(jié)幾種解決方案:
1.不使用@ResponseBody注解,使用HttpServeletResponse設(shè)置contentType屬性
@RequestMapping(value ="/rest/create/document") public void create(Document document, HttpServletRespone respone) { repoonse.setContentType("text/plain;charset='utf-8'"); response.write("中文string"); }
2.返回Response Entity object,設(shè)置contentType,例:
@RequestMapping(value = "/rest/create/document") public ResponseEntity<String> create(Document document, HttpServletRespone respone) { HttpHeaders responseHeaders = new HttpHeaders(); responseHeaders.add("Content-Type", "text/html; charset=utf-8"); Document newDocument = DocumentService.create(Document); String json = jsonSerializer.serialize(newDocument); return new ResponseEntity<String>(json, responseHeaders, HttpStatus.OK); }
3.使用produces屬性:
@RequestMapping(value = "/rest/create/document",produces= "text/plain;charset=UTF-8") //返回的內(nèi)容類型 @ResponseBody public String create(Document document, HttpServletRespone respone) throws UnsupportedEncodingException { Document newDocument = DocumentService.create(Document); return jsonSerializer.serialize(newDocument); }
@RequestMapping
參數(shù)綁定(@RequestParam、 @RequestBody、 @RequestHeader 、 @PathVariable)
package org.springframework.web.bind.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.web.bind.annotation.Mapping; import org.springframework.web.bind.annotation.RequestMethod; @Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Mapping public @interface RequestMapping { String name() default ""; String[] value() default {}; RequestMethod[] method() default {}; String[] params() default {}; String[] headers() default {}; String[] consumes() default {}; String[] produces() default {}; }
RequestMapping是一個(gè)用來處理請求地址映射的注解,可用于類或方法上。用于類上,表示類中的所有響應(yīng)請求的方法都是以該地址作為父路徑。
RequestMapping注解有六個(gè)屬性。
1、value, method;
value: 指定請求的實(shí)際地址,指定的地址可以是URI Template 模式(后面將會(huì)說明);
method: 指定請求的method類型, GET、POST、PUT、DELETE等;
2、consumes,produces;
consumes: 指定處理請求的提交內(nèi)容類型(Content-Type),例如application/json, text/html;
produces: 指定返回的內(nèi)容類型,僅當(dāng)request請求頭中的(Accept)類型中包含該指定類型才返回;
3、params,headers;
params: 指定request中必須包含某些參數(shù)值是,才讓該方法處理。
headers: 指定request中必須包含某些指定的header值,才能讓該方法處理請求。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于java中的null類型---有關(guān)null的9件事
這篇文章主要介紹了java中的null類型---有關(guān)null的9件事,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08MyBatis 自動(dòng)更新時(shí)間的方法實(shí)現(xiàn)
本文主要介紹了MyBatis 自動(dòng)更新時(shí)間的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06SpringBoot+Email發(fā)送郵件的實(shí)現(xiàn)示例
Spring?Boot提供了簡單而強(qiáng)大的郵件發(fā)送功能,本文主要介紹了SpringBoot+Email發(fā)送郵件的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03SpringBoot定義過濾器、監(jiān)聽器、攔截器的方法
本篇文章主要介紹了SpringBoot定義過濾器、監(jiān)聽器、攔截器的方法,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-04-04SpringBoot實(shí)現(xiàn)接口版本控制的示例代碼
這篇文章主要介紹了springboot如何實(shí)現(xiàn)接口版本控制,接口版本控制,比如微服務(wù)請求中某個(gè)接口需要升級,正常做法是升級我們的版本,文中有詳細(xì)的代碼示例供大家參考,具有一定的參考價(jià)值,需要的朋友可以參考下2024-03-03一步步教你如何使用Java實(shí)現(xiàn)WebSocket
websocket協(xié)議是基于TCP的一種新的網(wǎng)絡(luò)協(xié)議,它實(shí)現(xiàn)了瀏覽器與服務(wù)器的全雙工通訊-允許服務(wù)器主動(dòng)發(fā)起信息個(gè)客戶端,websocket是一種持久協(xié)議,http是非持久協(xié)議,下面這篇文章主要給大家介紹了關(guān)于如何使用Java實(shí)現(xiàn)WebSocket的相關(guān)資料,需要的朋友可以參考下2023-05-05Java 讀取網(wǎng)絡(luò)圖片存儲(chǔ)到本地并生成縮略圖
用Java做開發(fā)經(jīng)常需要處理圖片。本文就來看一下如何保存圖片到本地并生成縮略圖2021-05-05SpringBoot3.0+SpringSecurity6.0+JWT的實(shí)現(xiàn)
本文主要介紹了SpringBoot3.0+SpringSecurity6.0+JWT的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-11-11