XML 在 JBoss 7.5.0 上使用 REST 服务时文件处理不正确

XML file is processed incorrectly when using REST service on JBoss 7.5.0

我们使用 JBoss EAP 6.4。0.GA 来部署我们的应用程序。

我们有一个定义如下的 REST 服务方法:

@POST
@Path("/test")
@Consumes("application/xml")
Response createTest(Test test, @Context UriInfo uriInfo);

Test 是用于映射传入 xml 文件的 class。

传入的 XML 文件保存为 "ISO-8859-1" 文件,在其中我们定义编码如下

<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?> 

使用 "regular" 个字符一切正常,当我们在 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 修复它。

非常感谢您的帮助。

我们最终更改了服务方法的签名:我们传递字节数组而不是字符串

@POST
@Path("/test")
@Consumes("application/xml")
Response createTest(byte[] xml, @Context UriInfo uriInfo);

然后自己创建String对象:

String xmlAsString = new String(xml, "ISO-8859-1");