您的位置:首页 > 编程语言 > Java开发

学习笔记--SpringMVC 3.1下返回json时中文显示乱码问题的解决方案

2013-09-18 10:58 666 查看
pring返回json时中文显示乱码的问题,网络上大多数的方法在Spring 3.1下都失效了。搞不懂Spring怎么不修正这个问题呢?
多费周折最终还是找到解决方案,并亲测通过,故分享之。
简单的说就是新建个转换类再注入。就那么简单,这就是开源的好处啊!

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

转换类: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; 
        } 
    } 
} 

另外写好后可能出现一些问题,因为我解决这个问题的时候就出了下面一个问题:

The prefix "mvc" for element "mvc:annotation-driven" is not bound

解决办法在xml的beans中添加 xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" 另外上面是spring版本需要和你包的版本一致 不然会报错。 网上找了很多方法,这个是我研究后解决的方法,可以用的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: