六月澎湃 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 arduino控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题