SpringMVC ajax JSON 中文亂碼問題

轉載自:http://xyly624.blog.51cto.com/842520/893606

 http://blog.csdn.net/dyllove98/article/details/9631553

由於之前一直都是研發自己的MVC和動態表單架構,最近因爲一個技術研究的項目,開始接觸Spring MVC。各種標註帶來了很好的開發感受,但是今天被AJAX的json返回折磨了好久。一直都是亂碼。嘗試了網上的各種方法,都不理想。

       最後通過研究StringHttpMessageConverter源代碼發現,開發者很坑的使用了"ISO-8859-1"作爲默認編碼。這種西歐編碼在如今的開源屆也不多見,不知道是不是Spring一直忘記改了。

public class StringHttpMessageConverter extends AbstractHttpMessageConverter<String> {  
  
    public static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");  
  
    private Charset getContentTypeCharset(MediaType contentType) {  
        if (contentType != null && contentType.getCharSet() != null) {  
            return contentType.getCharSet();  
        }  
        else {  
            return DEFAULT_CHARSET;  
        }  
    }  

   發現這段之後,就好處理了,由於DEFAULT_CHARSET無法通過IoC進行修改,最直接的方式就是完成自己的AbstractHttpMessageConverter,命名爲:UTF8StringHttpMessageConverter。邏輯和StringHttpMessageConverter 一致,只需將DEFAULT_CHARSET修改爲UTF-8即可。

spring-servlet 配置

<mvc:annotation-driven> 
    <mvc:message-converters register-defaults="true"> 
        <bean class="com.abc.spring.UTF8StringHttpMessageConverter"/> 
    </mvc:message-converters> 
</mvc:annotation-driven> 

controller中

 @RequestMapping(value = "/getWeather",method = {RequestMethod.POST,RequestMethod.GET},produces="text/plain;charset=UTF-8")
    @ResponseBody



轉換類:

public class UTF8StringHttpMessageConverter extends 
        AbstractHttpMessageConverter<String> { 
 
    public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8"); 
    private final List<Charset> availableCharsets; 
 
    public UTF8StringHttpMessageConverter() { 
        this(DEFAULT_CHARSET); 
    } 
 
    public UTF8StringHttpMessageConverter(Charset defaultCharset) { 
        super(new MediaType("text", "plain", defaultCharset), MediaType.ALL); 
        this.availableCharsets = new ArrayList<Charset>(Charset 
                .availableCharsets().values()); 
    } 
 
    @Override 
    protected boolean supports(Class<?> clazz) { 
        return String.class.equals(clazz); 
    } 
 
    @Override 
    protected String readInternal(Class<? extends String> clazz, 
            HttpInputMessage inputMessage) throws IOException, 
            HttpMessageNotReadableException { 
        MediaType contentType = inputMessage.getHeaders().getContentType(); 
        Charset charset = contentType.getCharSet() != null ? contentType 
                .getCharSet() : DEFAULT_CHARSET; 
        return FileCopyUtils.copyToString(new InputStreamReader(inputMessage 
                .getBody(), charset)); 
    } 
 
    @Override 
    protected void writeInternal(String t, HttpOutputMessage outputMessage) 
            throws IOException, HttpMessageNotWritableException { 
        MediaType contentType = outputMessage.getHeaders().getContentType(); 
        Charset charset = contentType.getCharSet() != null ? contentType 
                .getCharSet() : DEFAULT_CHARSET; 
        FileCopyUtils.copy(t, new OutputStreamWriter(outputMessage.getBody(), 
                charset)); 
    } 
 
    protected List<Charset> getAcceptedCharsets() { 
        return this.availableCharsets; 
    } 
     
    @Override 
    protected Long getContentLength(String s, MediaType contentType) { 
        if (contentType != null && contentType.getCharSet() != null) { 
            Charset charset = contentType.getCharSet(); 
            try { 
                return (long) s.getBytes(charset.name()).length; 
            } catch (UnsupportedEncodingException ex) {                 
                throw new InternalError(ex.getMessage()); 
            } 
        } else { 
            return null; 
        } 
    } 
} 


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