2 it158 it158 于 2014.07.25 00:33 提问

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个回答

Rnighk
Rnighk   2014.08.12 18: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 "中文乱码";
}
xxlrcxx
xxlrcxx 比网上其他方法方便多了!!赞一个
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片