@RequestBody将复杂JSON映射到简单对象

时间:2018-02-18 10:31:28

标签: java json rest jackson mapping

POJO课程:

public class Transaction implements Serializable {

    @Id
    @GeneratedValue
    private Long id;
    private String userName;
    private String apiKey;
    private int amount;
    private String accountNumber;
    private String expirationDate;
    private String cscCode;
    private String customerAccountCode;
    private String firstName;
    private String lastName;
    private String street;
    private String city;
    private String state;
    private String zipCode;
    private String phoneNumber;
    private String description;
    private String responseCode;
    private String responseMessage;
    private String authCode;
    private String avsResultCode;
    private String cvvResultCode;


}

控制器:

@PostMapping
@ResponseStatus(OK)
public @ResponseBody ResponseEntity<transactionDto> 
        createTransaction(@RequestBody TransactionDto transactionDto) {

    System.out.println(transactionDto);

    return new ResponseEntity<Object>(transactionDto, OK);
}

JSON请求:

{
    "createTransactionRequest": {
        "merchantAuthentication": {
            "name": "8r6dGL5hRZ",
            "transactionKey": "72Ybq7QdQf475FjJ"
        },
        "refId": "123456",
        "transactionRequest": {
            "transactionType": "authCaptureTransaction",
            "amount": "5",
            "payment": {
                "creditCard": {
                    "cardNumber": "5424000000000015",
                    "expirationDate": "2020-12",
                    "cardCode": "999"
                }
            },
            "lineItems": {
                "lineItem": {
                    "itemId": "1",
                    "name": "vase",
                    "description": "Cannes logo",
                    "quantity": "18",
                    "unitPrice": "45.00"
                }
            },
            "tax": {
                "amount": "4.26",
                "name": "level2 tax name",
                "description": "level2 tax"
            },
            "duty": {
                "amount": "8.55",
                "name": "duty name",
                "description": "duty description"
            },
            "shipping": {
                "amount": "4.26",
                "name": "level2 tax name",
                "description": "level2 tax"
            },
            "poNumber": "456654",
            "customer": {
                "id": "99999456654"
            },
            "billTo": {
                "firstName": "Ellen",
                "lastName": "Johnson",
                "company": "Souveniropolis",
                "address": "14 Main Street",
                "city": "Pecan Springs",
                "state": "TX",
                "zip": "44628",
                "country": "USA"
            },
            "shipTo": {
                "firstName": "China",
                "lastName": "Bayles",
                "company": "Thyme for Tea",
                "address": "12 Main Street",
                "city": "Pecan Springs",
                "state": "TX",
                "zip": "44628",
                "country": "USA"
            },
            "customerIP": "192.168.1.1",
            }
        }
    }
}

问题:

当我处理请求时,我收到一个空的TransactionDto对象。

的System.out.println(transactionDto)

TransactionDto(id = null,userName = null,apiKey = null,amount = 0,accountNumber = null,expirationDate = null,cscCode = null,customerAccountCode = null,firstName = null,lastName = null,street = null,city = null,state = null,zipCode = null,phoneNumber = null,description = null)

如何将嵌套的JSON请求映射到Jackson的简单TransactionDto对象?或者其他一些? 谢谢!

2 个答案:

答案 0 :(得分:1)

尝试在TransactionDto上注释@XmlRootElement

答案 1 :(得分:1)

Transaction类与TransactionDto相同吗?

如果是这样,您可以在构造函数中添加@JsonCreator注释,并将此请求转换为所需的类。

public class Transaction {
     // Using map to collect all properties at once
     @JsonCreator
     public Transaction(Map<String, Object> properties) {
          this.name = properties.get("createTransactionRequest.merchantAuthentication.name");
     ...
     }

     // Using property annotation to specify
     @JsonCreator
     public Transaction(@JsonProperty("createTransactionRequest.merchantAuthentication.name") String name, ...) {
          this.name = name;
     ...
     }

}

更多详情:http://www.cowtowncoder.com/blog/archives/2011/07/entry_457.html