在JavaREST服务中将JSON响应映射到抽象类型
我在一家电子商务公司工作,我正在整合3种不同的支付网关。它们都需要回调URL来回发事务状态 我已经定义了一个资源来存储事务的状态在JavaREST服务中将JSON响应映射到抽象类型,java,rest,jersey,json-deserialization,paytm,Java,Rest,Jersey,Json Deserialization,Paytm,我在一家电子商务公司工作,我正在整合3种不同的支付网关。它们都需要回调URL来回发事务状态 我已经定义了一个资源来存储事务的状态 http://www.api.com/api/users/{userid}/order/{orderId}/payments/{paymentModeId}/paymentStatus 我定义了一个名为IPaymentStatusResponse的接口,并创建了3个实现。基于uri路径中的paymentModeId,将选择适当的实现来持久化事务状态 例如:三个不同网
http://www.api.com/api/users/{userid}/order/{orderId}/payments/{paymentModeId}/paymentStatus
我定义了一个名为IPaymentStatusResponse的接口,并创建了3个实现。基于uri路径中的paymentModeId,将选择适当的实现来持久化事务状态
例如:三个不同网关的回调url如下所示
支付模式1-paytm,支付模式2-payu,支付模式3-cc大道
http://www.api.com/api/users/300/order/501/payments/1/paymentStatus
http://www.api.com/api/users/300/order/501/payments/2/paymentStatus
http://www.api.com/api/users/300/order/501/payments/3/paymentStatus
方法签名
public void createPaymentStatus(
@PathParam("paymentModeId") int paymentModeId,
IPaymentStatusResponse response) throws MyAppException {
paymentServiceImpl.createPaymentResponse(response, paymentModeId);
}
这是正确的方法吗
在执行HTTP post时,出现以下错误:
Can not construct instance of com.myapp.dto.payments.IPaymentStatusResponse, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information
at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@1ee3ab21; line: 1, column: 1]
另外,我必须为所有不同的网关定义不同的端点,并映射响应对象
更新:
这里可以找到一个很好的解释
它需要响应json中的type元素来选择具体的类。
示例json,其中提到了类型和我的接口配置。这很有效。但不确定如何处理,因为响应json不在我的控制之下,它来自支付网关提供商
{
"MID":"abc",
"TXNID":"T123",
"ORDERID":"100",
"BANKTXNID":"B123",
"TXNAMOUNT":"1",
"CURRENCY":"INR",
"STATUS":"TXN_SUCCESS",
"RESPCODE":"01",
"RESPMSG":"Txn Success",
"TXNDATE":"2015-12-14 02:10:29.742447",
"GATEWAYNAME":"ICICI",
"BANKNAME":"ICICI",
"PAYMENTMODE":"CC",
"type":"PayTMPaymentResponse",
"CHECKSUMHASH":"ggg"
}
接口
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @Type(value = PayTMPaymentResponse.class, name = "PayTMPaymentResponse") })
public interface IPaymentStatusResponse {
}
这可以通过一些查询或路径参数来实现吗?Spring/Jackson无法自动反序列化。 您必须为给定类型提供自定义反序列化程序。 我认为,为共享信息和3种策略做一个POJO应该更容易 如果你用的是jackson的话,这会有帮助的。我认为其他LIB也有类似的情况。