我在RESTeasy中编码字符串时遇到了问题。问题是中文字母表或任何其他非拉丁字符不能正确显示。如果我尝试将它们打印出来(或作为响应发送),我会得到“?”而不是。我相信RESTeasy中的默认编码是us-ascii。你知道怎么把它改成UTF-8吗?也许这个问题还有其他的解决方案?
下面是我的代码中的一小段:
@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上。
谢谢!
发布于 2013-02-06 16:06:39
这与resteasy或jax-rs mate无关。
有几件事你可能想要研究一下:
tomcat
上
发布于 2014-01-31 06:55:51
对于那些因为问题的标题而来到这里的人,这里有一个解决方案。在@Consumes注释中,尝试将字符集设置为UTF-8:
@Consumes(MediaType.MULTIPART_FORM_DATA+";charset=UTF-8");发布于 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自定义拦截器来解决这个问题:
但是这个拦截器实际上已经被弃用了,它宣称要使用jaxrs 2.0拦截器机制。
长话短说,为了让它以一种独立于实现的方式工作,您可以创建这个类:
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>
`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.https://stackoverflow.com/questions/14683677
复制相似问题