六月澎湃 2014-07-24 16:33 采纳率: 0%
浏览 8824

springMVC 3.2 接收和返回json 中文乱码问题

折腾了一星期啦,springMVC处理含有中文的json参数乱码,按照网上的各种尝试,改配置的,自己写消息转换器的,各种尝试均失败,不知道哪位有实际经验供借鉴下呢

1、按照网上同行的说法改的配置:
class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">


class="org.springframework.http.converter.StringHttpMessageConverter">


text/plain;charset=UTF-8



class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">


text/plain;charset=UTF-8
application/json;charset=UTF-8





<mvc:annotation-driven/>

2、自己写的转换类
public class UTF8StringHttpMessageConverter extends AbstractHttpMessageConverter{

public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");

private final Charset defaultCharset;

private final List<Charset> availableCharsets;

private boolean writeAcceptCharset = true;

private static final MediaType utf8 = new MediaType("text", "plain", Charset.forName("UTF-8"));  

/**
 * A default constructor that uses {@code "ISO-8859-1"} as the default charset.
 * @see #StringHttpMessageConverter(Charset)
 */
public UTF8StringHttpMessageConverter() {
    this(DEFAULT_CHARSET);
}

/**
 * A constructor accepting a default charset to use if the requested content
 * type does not specify one.
 */
public UTF8StringHttpMessageConverter(Charset defaultCharset) {
    super(new MediaType("text", "plain", DEFAULT_CHARSET), MediaType.ALL);
    this.defaultCharset = DEFAULT_CHARSET;
    this.availableCharsets = new ArrayList<Charset>(Charset.availableCharsets().values());
}

/**
 * Indicates whether the {@code Accept-Charset} should be written to any outgoing request.
 * <p>Default is {@code true}.
 */
public void setWriteAcceptCharset(boolean writeAcceptCharset) {
    this.writeAcceptCharset = writeAcceptCharset;
}

@Override
public boolean supports(Class<?> clazz) {
    return String.class.equals(clazz);
}

@Override
protected String readInternal(Class<? extends String> clazz, HttpInputMessage inputMessage) throws IOException {
    Charset charset = getContentTypeCharset(inputMessage.getHeaders().getContentType());
    return FileCopyUtils.copyToString(new InputStreamReader(inputMessage.getBody(), charset));
}

@Override
protected Long getContentLength(String s, MediaType contentType) {
    Charset charset = getContentTypeCharset(contentType);
    try {
        return (long) s.getBytes(charset.name()).length;
    }
    catch (UnsupportedEncodingException ex) {
        // should not occur
        throw new IllegalStateException(ex);
    }
}

@Override
protected void writeInternal(String s, HttpOutputMessage outputMessage) throws IOException {
    if (this.writeAcceptCharset) {
        outputMessage.getHeaders().setAcceptCharset(getAcceptedCharsets());
    }
    Charset charset = getContentTypeCharset(outputMessage.getHeaders().getContentType());
    FileCopyUtils.copy(s, new OutputStreamWriter(outputMessage.getBody(), charset));
}

/**
 * Return the list of supported {@link Charset}.
 * <p>By default, returns {@link Charset#availableCharsets()}. Can be overridden in subclasses.
 * @return the list of accepted charsets
 */
protected List<Charset> getAcceptedCharsets() {
    return this.availableCharsets;
}

private Charset getContentTypeCharset(MediaType contentType) {
    if (contentType != null && contentType.getCharSet() != null) {
        return contentType.getCharSet();
    }
    else {
        return DEFAULT_CHARSET;
    }
}

protected MediaType getDefaultContentType(String dumy) {  
    return utf8;  
}  

}

这些尝试都不行的,哪位遇到过呢,高分求助~

  • 写回答

1条回答

  • 穹源 2014-08-12 10:04
    关注

    在RequestMapping中加入 produces="text/html;charset=UTF-8"即可,例如

    @RequestMapping(value="/testGarbled.do", produces="text/html;charset=UTF-8" )
    @ResponseBody
    public String testGarbled(ModelMap modelMap) {
        return "中文乱码";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 链接问题 C++LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接