首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将RESTeasy中的编码设置为UTF-8?

如何将RESTeasy中的编码设置为UTF-8?
EN

Stack Overflow用户
提问于 2013-02-04 17:21:12
回答 6查看 15.1K关注 0票数 3

我在RESTeasy中编码字符串时遇到了问题。问题是中文字母表或任何其他非拉丁字符不能正确显示。如果我尝试将它们打印出来(或作为响应发送),我会得到“?”而不是。我相信RESTeasy中的默认编码是us-ascii。你知道怎么把它改成UTF-8吗?也许这个问题还有其他的解决方案?

下面是我的代码中的一小段:

代码语言:javascript
复制
@POST
@Path("post")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String uploadData(@MultipartForm DataUploadForm uploadForm) {

...

//the code below is just for the purpose of presentation

String text = "some non-latin alphabet signs here"
System.out.println(text);  // "??????" is printed out

return text; //"??????" is returned
}

我的resteasy-jaxrs-3.0-beta-2运行在Tomcat7.0上。

谢谢!

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-02-06 16:06:39

这与resteasy或jax-rs mate无关。

检查this post

有几件事你可能想要研究一下:

tomcat

  • 如果数据来自数据库,则需要确保数据库中的数据是使用utf8

  • 编码的,因为集成开发环境和tomcat可能运行在不同的configurations.

票数 0
EN

Stack Overflow用户

发布于 2014-01-31 06:55:51

对于那些因为问题的标题而来到这里的人,这里有一个解决方案。在@Consumes注释中,尝试将字符集设置为UTF-8:

代码语言:javascript
复制
@Consumes(MediaType.MULTIPART_FORM_DATA+";charset=UTF-8");
票数 10
EN

Stack Overflow用户

发布于 2016-08-31 15:52:20

我在将jaxrs1 (jersey)迁移到RESTEasy3.0.17.final时遇到了同样的问题。

当从自定义客户端上传form/multipart时,客户端确实发送了content-type,但没有字符集,然后resteasy假定为"us-ascii“(根据文档,根据MIME RFC )。有趣的是,如果根本没有给出content-type,resteasy就会假设"text/plain;charset=ISO-8859-1“(我假设遵循http规范?)。

他们的文档建议使用RestEasy自定义拦截器来解决这个问题:

https://docs.jboss.org/resteasy/docs/3.0.2.Final/userguide/html/Multipart.html#multipart_overwrite_content_type

但是这个拦截器实际上已经被弃用了,它宣称要使用jaxrs 2.0拦截器机制。

长话短说,为了让它以一种独立于实现的方式工作,您可以创建这个类:

代码语言:javascript
复制
package x.y.z;
import java.io.IOException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.ext.Provider;
import javax.ws.rs.ext.ReaderInterceptor;
import javax.ws.rs.ext.ReaderInterceptorContext;
@Provider
/**
 * If charset not given (form multipart upload), default to UTF-8 and not us-ascii (MIME RFC).
 */
public class RestEasyDefaultCharsetInterceptor implements ReaderInterceptor {
    // Using string value instead of constant to limit references to RestEasy (this should be possible to set through web.xml imo)
    // private static final String RESTEASY_DEFAULT_CHARSET_PROPERTY = org.jboss.resteasy.plugins.providers.multipart.InputPart.DEFAULT_CHARSET_PROPERTY;
    private static final String RESTEASY_DEFAULT_CHARSET_PROPERTY = "resteasy.provider.multipart.inputpart.defaultCharset";
    @Override
    public Object aroundReadFrom(ReaderInterceptorContext ctx) throws IOException, WebApplicationException {
        ctx.setProperty(RESTEASY_DEFAULT_CHARSET_PROPERTY, "UTF-8");
        return ctx.proceed();
    }
}

接下来,将拦截器添加到web.xml:<context-param> <param-name>resteasy.providers</param-name> <param-value>

代码语言:javascript
复制
         `x.y.z.RestEasyDefaultCharsetInterceptor         </param-value>      </context-param>`  Now, I don't fully understand the entire mechanism yet, but you _should_ also be able to fix the problem on the client side by specifying the charset parameter to the content-type.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14683677

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档