Reputation: 879
Requestbody is not getting mapped to the Object that i used here. All the fields in PaymentRequest are coming as null. I see annotations and mapings everything seems to be correct.
@RestController
@RequestMapping("/pay")
public class PaymentServiceAPIImpl {
@RequestMapping(value = "/request", method = RequestMethod.POST)
public Response submitPaymentRequest(@RequestBody PaymentRequest paymentRequest) {
System.out.println(paymentRequest.getClientId()); // here I am getting all the fields are null
return Response.ok().build();
}
}
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "payment_request", namespace = "http://www.abc-services.com/payment_request", propOrder = { "currencyCode", "clientId" })
public class PaymentRequest implements Serializable {
private static final long serialVersionUID = 1L;
@XmlElement(name = "currency_code")
protected String currencyCode;
@XmlElement(name = "client_id")
protected String clientId;
public String getCurrencyCode() {
return currencyCode;
}
public void setCurrencyCode(String value) {
this.currencyCode = value;
}
public String getClientId() {
return clientId;
}
public void setClientId(String value) {
this.clientId = value;
}
}
Here is the request
curl -X POST \
http://localhost:8080/pay/request \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-H 'Postman-Token: fead689c-239-284bb2116ae2' \
-d '{
"payment_token": {
"client_id": "cyber",
"currency_code": "USD"
}
}'
getting paymentrequest like this into the controller:
PaymentRequest {
clientId: null,
cardType: null,
cardIssuer: null
}
Any pointers why the request is not mapped to the PaymentRequest?
Upvotes: 1
Views: 4006
Reputation: 557
Your request in JSON but Parameter is XML. Something change:
curl -X POST \
http://localhost:8080/pay/request \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-H 'Postman-Token: fead689c-239-284bb2116ae2' \
-d '{
"clientId": "cyber",
"currencyCode": "USD"
}'
public class PaymentRequest {
protected String currencyCode;
protected String clientId;
public String getCurrencyCode() {
return currencyCode;
}
public void setCurrencyCode(String value) {
this.currencyCode = value;
}
public String getClientId() {
return clientId;
}
public void setClientId(String value) {
this.clientId = value;
}
}
Upvotes: 0
Reputation: 159086
You code doesn't map the data because the JSON is an object with a single property named payment_token
, and you parameter type PaymentRequest
doesn't have a property of that name.
Either change payload to:
{
"client_id": "cyber",
"currency_code": "USD"
}
Or change parameter type to use this class instead:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "payment_wrapper", namespace = "http://www.abc-services.com/payment_wrapper", propOrder = { "paymentToken" })
public class PaymentWrapper {
@XmlElement(name = "payment_token")
protected PaymentRequest paymentToken;
public PaymentRequest getPaymentToken() {
return paymentToken;
}
public void setPaymentToken(PaymentRequest value) {
this.paymentToken = value;
}
}
Upvotes: 3