在REST Web服务中使用Content-Type“application / x-www-form-urlencoded”挣扎

时间:2013-07-05 08:22:16

标签: rest cxf content-type

提前感谢您就CXF REST提供商面临的以下问题提供建议。

我使用Apache CXF开发了REST Web服务服务器。以下是合同定义。请注意,这用于Camel上下文。

public class PaymentSandboxService {

    @POST
    @Consumes({"application/json", "application/x-www-form-urlencoded"})
    @Produces({"application/json", "application/x-www-form-urlencoded"})
    @Path("/2_1/payment/{endUserId}/transactions/amount")
    public Response charge(@Body
    final AmountTransaction amountTransaction, @PathParam("endUserId")
    final String endUserId) throws IOException {

        return null;
    }
}

以下是我的bean定义:

    <cxf:rsServer id="rsServer" address="/{{publicAddress}}"  serviceClass="a.b.cPaymentSandboxService"
        loggingFeatureEnabled="true">
        <cxf:providers>
            <ref bean="jsonProvider" />
            <ref bean="formUrlEncodeProvider" />
        </cxf:providers>
    </cxf:rsServer>


<bean id="jsonProvider" class="org.apache.cxf.jaxrs.provider.JSONProvider">
  <property name="marshallAsJaxbElement" value="true" />
</bean>
<bean id="formUrlEncodeProvider" class="org.apache.cxf.jaxrs.provider.FormEncodingProvider" />

当我使用Content-Type'application / json'发送请求时,一切正常。但是当使用Content-Type'application / x-www-form-urlencoded'发送请求时,请求命中服务器但是在将body转换为所需数据类型时失败。以下是我在Fuse esb log中获取的日志部分。

日志的一部分:

ID: 17
Address: http://cnb69:8181/cxf/paymentsandbox/2_1/payment/tel:+916309700000/transactions/amount
Encoding: ISO-8859-1
Http-Method: POST
Content-Type: application/x-www-form-urlencoded
Headers: {Accept=[application/json], accept-encoding=[gzip,deflate], Authorization=[Basic a2FzdW5wYXlzYW5kYm94OnBANTV3MHJk], connection=[keep-alive], Content-Length=[670], content-type=[application/x-www-form-urlencoded], Host=[cnb69:8181], User-Agent=[Apache-HttpClient/4.1.1 (java 1.5)]}
Payload: {
    "amountTransaction": {
        "clientCorrelator": "54321",
        "endUserId": "tel:+916309700000",
        "paymentAmount": {
            "chargingInformation": {
                "amount": "10.2662",
                "currency": "USD",
                "description": [
                    "Alien Invaders Game"
                ]
            },
            "chargingMetaData": {
                "onBehalfOf": "Example Games Inc",
                "purchaseCategoryCode": "Game",
                "channel": "SMS",
                "taxAmount": "0"
            }
        },
        "referenceCode": "REF-12345",
        "transactionOperationStatus": "CHARGED"
    }
}
--------------------------------------
2013-07-05 13:44:27,477 | WARN  | qtp1316166688-257    | org.apache.cxf.jaxrs.utils.JAXRSUtils | No message body reader has been found for request class AmountTransaction, ContentType : application/x-www-form-urlencoded.
2013-07-05 13:44:27,478 | INFO  | qtp1316166688-257    | org.apache.cxf.interceptor.AbstractLoggingInterceptor | Outbound Message
---------------------------
ID: 17
Response-Code: 415
Content-Type: text/xml
Headers: {Date=[Fri, 05 Jul 2013 08:14:27 GMT], Content-Length=[0]}
--------------------------------------

1 个答案:

答案 0 :(得分:1)

该片的副本(来自Sergey) http://cxf.547215.n5.nabble.com/Consume-quot-application-x-www-form-urlencoded-quot-Content-Type-in-REST-Web-Service-cxf-rsServer-td5730399.html#a5730446

不同的解决方案是:

  • 有一个Form或MultivaluedMap参数而不是AmountTransaction并手动填充AmountTransaction
  • 拥有自定义提供商(如您所建议)
  • 使用带有AmountTransaction
  • 的cxf扩展名@FormParam(“”)
  • 使用JAX-RS 2.0 @BeanParam并使用FormParam注释AmountTransaction属性