JAX-RS 客户端 PUT/POST 请求:无法在正文中访问实体(对象)
JAX-RS Client PUT/POST request: unable to an entity(Object) in the body
我使用 bog-standard JAX-RS 客户端代码如下(借自 https://www.baeldung.com/jersey-jax-rs-client
):
Client client = ClientBuilder.newClient();
Amount amount = new Amount("GBP", totalSavings);
Response response = client.target(TRANSFER_TO_SAVINGS_GOAL_REST_URI)
.resolveTemplate("accountUid", accountUid)
.resolveTemplate("savingsGoalUid", savingsUid)
.request()
.header("Accept", "application/json")
.header("Authorization", "Bearer " + customerToken)
.put(Entity.json(amount));
现在,失败了,如返回 400 Bad request。
在接收端,发送的实体(amount
)为空。知道为什么吗?
我正在检查调试器并且该实体有一个真实的值,但是如果我构建一个测试 @Post 端点如下:
@PutMapping(value = "/account/{accountUid}/savings-goals/{savingsGoalUid}/add-money/{transferUid}")
public Response getUserHistory(String amount,
@PathVariable("accountUid") UUID accountUid,
@PathVariable("savingsGoalUid") UUID savingsGoalUid,
@PathVariable("transferUid") UUID transferUid) {
log.info("Amount: " , amount);
return null;
}
确实确认金额为空。
有什么帮助吗?
一方面,Amount 是一个对象,另一方面,amount 是一个简单的字符串。您是否尝试过使用 getUserHistory
方法创建 Amount
class?
那么你可以这样做:
@PutMapping(value = "/account/{accountUid}/savings-goals/{savingsGoalUid}/add-money/{transferUid}")
public Response getUserHistory(Amount amount,
@PathVariable("accountUid") UUID accountUid,
@PathVariable("savingsGoalUid") UUID savingsGoalUid,
@PathVariable("transferUid") UUID transferUid) {
log.info("Amount: " , amount);
return null;
}
如果您希望收到 json 那么我认为将完整的 json 对象映射到字符串是行不通的
希望对您有所帮助!
希望对某人有所帮助的答案是:
最后一点:
...
Entity.json(amount);
没有为 Amount
生成所需的 JSON 结构。要求:
{
"amount": {
"currency": "GBP",
"minorUnits": 1
}
}
生成:
{ "currency": "GBP",
"minorUnits": 1
}
为了解决这个问题,我在 Amount
class:
中添加了以下 2 个注释
@JsonTypeName("amount")
@JsonTypeInfo(include = WRAPPER_OBJECT, use = NAME)
public class Amount {
private String currency;
private long minorUnits;
}
即生成所需的JSON格式。
为了检查请求中 JSON 的值,我使用了以下代码:
response.readEntity(String.class)
为 amount
打印了 JSON 字符串
我使用 bog-standard JAX-RS 客户端代码如下(借自 https://www.baeldung.com/jersey-jax-rs-client ):
Client client = ClientBuilder.newClient();
Amount amount = new Amount("GBP", totalSavings);
Response response = client.target(TRANSFER_TO_SAVINGS_GOAL_REST_URI)
.resolveTemplate("accountUid", accountUid)
.resolveTemplate("savingsGoalUid", savingsUid)
.request()
.header("Accept", "application/json")
.header("Authorization", "Bearer " + customerToken)
.put(Entity.json(amount));
现在,失败了,如返回 400 Bad request。
在接收端,发送的实体(amount
)为空。知道为什么吗?
我正在检查调试器并且该实体有一个真实的值,但是如果我构建一个测试 @Post 端点如下:
@PutMapping(value = "/account/{accountUid}/savings-goals/{savingsGoalUid}/add-money/{transferUid}")
public Response getUserHistory(String amount,
@PathVariable("accountUid") UUID accountUid,
@PathVariable("savingsGoalUid") UUID savingsGoalUid,
@PathVariable("transferUid") UUID transferUid) {
log.info("Amount: " , amount);
return null;
}
确实确认金额为空。
有什么帮助吗?
一方面,Amount 是一个对象,另一方面,amount 是一个简单的字符串。您是否尝试过使用 getUserHistory
方法创建 Amount
class?
那么你可以这样做:
@PutMapping(value = "/account/{accountUid}/savings-goals/{savingsGoalUid}/add-money/{transferUid}")
public Response getUserHistory(Amount amount,
@PathVariable("accountUid") UUID accountUid,
@PathVariable("savingsGoalUid") UUID savingsGoalUid,
@PathVariable("transferUid") UUID transferUid) {
log.info("Amount: " , amount);
return null;
}
如果您希望收到 json 那么我认为将完整的 json 对象映射到字符串是行不通的
希望对您有所帮助!
希望对某人有所帮助的答案是:
最后一点:
...
Entity.json(amount);
没有为 Amount
生成所需的 JSON 结构。要求:
{
"amount": {
"currency": "GBP",
"minorUnits": 1
}
}
生成:
{ "currency": "GBP",
"minorUnits": 1
}
为了解决这个问题,我在 Amount
class:
@JsonTypeName("amount")
@JsonTypeInfo(include = WRAPPER_OBJECT, use = NAME)
public class Amount {
private String currency;
private long minorUnits;
}
即生成所需的JSON格式。
为了检查请求中 JSON 的值,我使用了以下代码:
response.readEntity(String.class)
为 amount