我们使用JBoss EAP6.4.0.GA部署我们的应用程序。
我们有一个REST服务的方法,定义如下:
@POST
@Path("/test")
@Consumes("application/xml")
Response createTest(Test test, @Context UriInfo uriInfo);Test是用于映射传入的xml文件的类。
传入的XML文件保存为"ISO-8859-1“文件,并在其中定义编码如下
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> 有了“常规”字符,一切都能正常工作,当我们在xml中使用äöü时,问题就开始了。在这种情况下,当调用REST方法时,我们有一个异常。
ERROR [org.jboss.resteasy.resteasy_jaxrs.i18n] (http-/172.28.105.3:443-6) RESTEASY000100: Failed executing POST /test: org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException: javax.xml.bind.UnmarshalException
- with linked exception:
[org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 2 of 3-byte UTF-8 sequence.]
at org.jboss.resteasy.plugins.providers.jaxb.AbstractJAXBProvider.readFrom(AbstractJAXBProvider.java:147) [resteasy-jaxb-provider-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:106) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.read(GZIPDecodingInterceptor.java:63) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.core.interception.MessageBodyReaderContextImpl.proceed(MessageBodyReaderContextImpl.java:109) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:168) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:137) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:160) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:269) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:227) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:216) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:541) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:523) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:125) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.10.Final-redhat-1.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-2.jar:1.0.2.Final-redhat-2]我们尝试了多种方法来解决这个问题:
1)更改的方法签名,因此xml被接收为字符串,然后自己取消它,而不是依赖于JBoss :。
@POST
@Path("/test")
@Consumes("application/xml")
Response createTest(String xml, @Context UriInfo uriInfo);在本例中,我们以字符串的形式接收xml文件,但äöü被���替换。请注意,接下来我们尝试使用此方法进行更改。
2)在standalone.xml中设置系统属性如下:
<system-properties>
<property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/>
<property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="true"/>
</system-properties>没有结果。这里有���而不是���。
3)变更standalone.conf
设置JAVA_OPTS="$JAVA_OPTS -Dfile.encoding=UTF-8"
还是没有结果。
4) xml文件的更改注释
@Consumes("application/xml;charset=ISO-8859-1")在这种情况下-不支持媒体类型
5)将传入xml文件中的编码更改为UTF-8,并将它们保存为UTF-8.。
在这种情况下,一切都如预期的那样工作。但是最好将xml文件保存在ISO-8859-1中,因为它需要花费很多精力来更改工作流。
有人能给我们一个提示吗?我们下一步可以尝试什么?我相信肯定有办法解决ISO-8859-1的问题。
你的帮助会很感激的。
发布于 2015-10-22 16:51:51
我们最终更改了服务方法的签名:而不是字符串,而是传递字节数组。
@POST
@Path("/test")
@Consumes("application/xml")
Response createTest(byte[] xml, @Context UriInfo uriInfo);然后自己创建String对象:
String xmlAsString = new String(xml, "ISO-8859-1");https://stackoverflow.com/questions/33263257
复制相似问题