2012-05-28 77 views
1

我使用Apache CXF開發JAX-RS服務。我的服務正在運行,但我想將其更改爲接受壓縮(gzipped)請求。接受CXF中的gzip請求

我做了一些谷歌上搜索,並將此我beans.xml文件:

<bean id="GZIPInInterceptor" class="org.apache.cxf.transport.common.gzip.GZIPInInterceptor"/> 
<bean id="compressGZIPFeature" class="org.apache.cxf.transport.http.gzip.GZIPFeature"/> 
<cxf:bus> 
    <cxf:inInterceptors> 
     <ref bean="GZIPInInterceptor"/> 
    </cxf:inInterceptors> 
    <cxf:features> 
     <ref bean="compressGZIPFeature"/> 
     <cxf:logging/> 
    </cxf:features> 
</cxf:bus> 

但是當我用壓縮請求調用該服務從我的客戶端應用程序,我得到的錯誤無效的UTF-8開始字節0x8b,這在日誌文件:

28-May-2012 12:59:42 org.apache.cxf.interceptor.AbstractLoggingInterceptor log 
INFO: Inbound Message 
---------------------------- 
ID: 5 
Address: http://localhost:8080/ .../
Encoding: ISO-8859-1 
Http-Method: POST 
Content-Type: application/xml 
Headers: {Accept=[application/xml], connection=[Keep-Alive], content-encoding=[g 
zip], Content-Length=[246], content-type=[application/xml], expect=[100-continue 
], host=[localhost:8080]} 
Payload: ?  ? ý¢`?I?%&/m-{¦J§JÎÓt?`?$Ï[email protected]?ý-?-µ?ý?iG#)½*?-eVe][email protected]¦Ý?+¸Ì{´¢¸Ì{´ 
¢¸¦;?N'¸¯ ?\fd?l÷+J++?!?¬+??~|??"? ?´?ez?ÎMQ-?¹hw+¾Q?/ºi¼X^|÷Ц=¯>°Þ¸8z|͵?ú4}|· 
???¡-÷t+ÍÎgO?÷vv¸v÷w÷´¦ tg¦¸-/Õ+´Ý}¦¦w?¦+C¤/²®_·dþÚ¦²{;'??>³¶ßÚ+¦'¤?Ý|·õ¦±¦¦¦?O? 
¯Ý>¥¦?·°.w 
-------------------------------------- 
28-May-2012 12:59:43 org.apache.cxf.jaxrs.provider.AbstractJAXBProvider handleJA 
XBException 
WARNING: javax.xml.bind.UnmarshalException 
- with linked exception: 
[com.ctc.wstx.exc.WstxIOException: Invalid UTF-8 start byte 0x8b (at char #2, by 
te #-1)] 
     at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStrea 
mException(UnmarshallerImpl.java:426) 

... 

28-May-2012 12:59:43 org.apache.cxf.interceptor.AbstractLoggingInterceptor log 
INFO: Outbound Message 
--------------------------- 
ID: 5 
Response-Code: 400 
Content-Type: text/plain 
Headers: {Content-Type=[text/plain], Date=[Mon, 28 May 2012 11:59:43 GMT]} 
Payload: JAXBException occurred : Invalid UTF-8 start byte 0x8b (at char #2, byt 
e #-1). Invalid UTF-8 start byte 0x8b (at char #2, byte #-1). 
-------------------------------------- 

回答

2

答案是,攔截沒有得到從XML文件回升。我嘗試添加該註釋上面我的服務方法,並開始工作:

@org.apache.cxf.interceptor.InInterceptors (interceptors = {"org.apache.cxf.transport.common.gzip.GZIPInInterceptor" }) 
+1

你可以去更容易並使用@ org.apache.cxf.annotations.GZIP註釋。 :-) –

5

在CXF,你可以使用

@GZIP

註釋用於壓縮消息