簡體   English   中英

在JBoss 7.5.0上使用REST服務時XML文件處理不正確

[英]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文件的類。

傳入的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作為String接收,然后自己解組而不是依賴JBoss:

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

在這種情況下,我們將xml文件作為String接收,但是äöü被替換為``。 請注意,我們嘗試使用此方法進行的下一次更改。

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的方法。

您的幫助將不勝感激。

我們最終更改了服務方法的簽名:我們傳遞字節數組,而不是String。

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

然后自己創建String對象:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM