深入淺析SpringMVC中controller的字符編碼

本篇文章為大家展示了深入淺析Spring MVC中controller的字符編碼,內(nèi)容簡明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。

專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)夏邑免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了超過千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

在使用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)容就是深入淺析Spring MVC中controller的字符編碼,你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

分享標(biāo)題:深入淺析SpringMVC中controller的字符編碼
鏈接URL:http://muchs.cn/article10/isphdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、移動(dòng)網(wǎng)站建設(shè)、Google、營銷型網(wǎng)站建設(shè)、服務(wù)器托管定制開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管