接受CXF中的gzip压缩请求

时间:2012-05-28 12:08:18

标签: cxf

我正在使用Apache CXF开发JAX-RS服务。我的服务正在运行,但我想将其更改为接受压缩(gzipped)请求。

我已经完成了一些谷歌搜索并将其添加到我的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?`?$Ï?@?ý-?-µ?ý?iG#)½*?-eVe]f?@¦Ý?+¸Ì{´¢¸Ì{´
¢¸¦;?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 个答案:

答案 0 :(得分:5)

在CXF中,您可以使用

@GZIP

压缩消息的注释

答案 1 :(得分:3)

答案是拦截器没有从XML文件中获取。我尝试在我的服务方法上面添加这个注释,它开始工作:

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