gpt4 book ai didi

java - RequestBody 未映射到对象

转载 作者:行者123 更新时间:2023-11-30 06:06:13 27 4
gpt4 key购买 nike

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"
}
}'

将这样的付款请求放入 Controller :

PaymentRequest {
clientId: null,
cardType: null,
cardIssuer: null
}

是否有任何指示说明请求未映射到 PaymentRequest?

最佳答案

您的代码不会映射数据,因为 JSON 是一个具有名为 payment_token 的单个属性的对象,并且您的参数类型 PaymentRequest 没有以下属性那个名字。

将有效负载更改为:

{
"client_id": "cyber",
"currency_code": "USD"
}

或者更改参数类型以使用此类:

@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;
}
}

关于java - RequestBody 未映射到对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51275632/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com