简体   繁体   English

将JSON响应映射到JAVA rest Service中的抽象类型

[英]Mapping JSON response to an abstract type in JAVA rest Service

I am working with an ecommerce company and I am integrating with 3 different payment gateways. 我正在与一家电子商务公司合作,我正在与3个不同的支付网关集成。 All of them require a callbackurl to post back the transaction status. 所有这些都需要callbackurl来回发交易状态。

I have defined a resource to store the status of the transaction 我已经定义了一个资源来存储事务的状态

http://www.api.com/api/users/{userid}/order/{orderId}/payments/{paymentModeId}/paymentStatus

I have defined an interface called IPaymentStatusResponse and have created 3 implementations. 我已经定义了一个名为IPaymentStatusResponse的接口,并创建了3个实现。 Based on the paymentModeId in the uri path, appropriate implementation will be picked up to persist the transaction status. 根据uri路径中的paymentModeId,将选择适当的实现来保持事务状态。

eg: callback url for three different gatewyas will look like this payment mode 1 - paytm, payment mode 2 - payu ,payment mode 3- cc avenue. 例如:三个不同的gatewyas的回调网址将看起来像这种支付模式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

Method signature 方法签名

public void createPaymentStatus(
            @PathParam("paymentModeId") int paymentModeId,
            IPaymentStatusResponse response) throws MyAppException {
        paymentServiceImpl.createPaymentResponse(response, paymentModeId);
}

Is this the correct way to approach this? 这是接近这个的正确方法吗?

When I did a HTTP post I get the following error: 当我发布HTTP帖子时,我收到以下错误:

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]

Other option , I ll have to define different end points for all the different gateways and map the response object. 其他选项,我必须为所有不同的网关定义不同的端点并映射响应对象。

UPDATE: 更新:

A good explanation can be found here http://programmerbruce.blogspot.in/2011/05/deserialize-json-with-jackson-into.html 可以在这里找到一个很好的解释http://programmerbruce.blogspot.in/2011/05/deserialize-json-with-jackson-into.html

It needs a type element in the response json to pick the concrete class. 它需要响应json中的一个type元素来选择具体类。 Sample json with type mentioned and configuration of my interface. 示例提到json类型和我的界面配置。 This works. 这有效。 But not sure how to handle this because the response json is not under my control and it comes from the payment gateway providers 但不知道如何处理这个因为响应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"
}

Interface 接口

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({ @Type(value = PayTMPaymentResponse.class, name = "PayTMPaymentResponse") })
public interface IPaymentStatusResponse {

}

Can this be implemented with some query or path parameter ? 可以使用一些查询或路径参数来实现吗?

Spring/Jackson is not able to deserialize automatically. Spring / Jackson无法自动反序列化。 You'll have to provide custom deserializer for a given type. 您必须为给定类型提供自定义反序列化程序。 I think it should be easier to do one POJO for shared information and 3 strategies. 我认为为共享信息和3个策略做一个POJO应该更容易。

it should help in case if you're using jackson. 如果你使用杰克逊,它应该会有所帮助。 I think other libs have something similar to that. 我认为其他libs有类似的东西。 http://wiki.fasterxml.com/JacksonPolymorphicDeserialization http://wiki.fasterxml.com/JacksonPolymorphicDeserialization

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM