六月澎湃 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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器