【发布时间】:2018-07-11 00:31:19
【问题描述】:
Requestbody 没有映射到我在这里使用的对象。 PaymentRequest 中的所有字段都为空。我看到注释和映射一切似乎都是正确的。
@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;
}
}
这是请求
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"
}
}'
将这样的支付请求发送到控制器中:
PaymentRequest {
clientId: null,
cardType: null,
cardIssuer: null
}
请求没有映射到 PaymentRequest 的任何指针?
【问题讨论】:
-
您使用的是 XML ObjectMapper 吗?为什么要使用 java.xml 注释?
-
您正在发布应用程序/json,但您的模型正在定义 XML 映射。
-
@SotiriosDelimanolis Spring MVC 使用映射实现很常见,该实现可以使用 XML 注释来定义 JSON 属性名称,允许代码以最少的定义共同支持 JSON 和 XML,从而允许客户端通过使用
Accept和Content-Type标头来决定他们更喜欢 JSON 还是 XML。
标签: java rest spring-mvc