![](/img/trans.png)
[英]Java Spring Boot how to know client receive the response from API
[英]Receive an urlenconded in Spring Boot API
我需要接收來自第三方 API 的 webhook 的請求。
帖子內容是以下格式的 urlenconded:
event=invoice.created&data%5Bid%5D=value1&data%5Bstatus%5D=pending&data%5Baccount_id%5D=value2
問題是用這些方括號序列化這個參數數據[id]。 我在 spring 啟動時遇到錯誤:
Invalid property 'data[account_id]' of bean class [br.com.bettha.domain.dto.IuguWebhookDto]: Property referenced in indexed property path 'data[account_id]' is neither an array nor a List nor a Map; 返回值為 [IuguDataDto(id=null, account_id=null, status=null, subscription_id=null)]
我的 controller:
@PostMapping(value = "/subscription-invoice", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
@ApiOperation(
value="Create a subscription invoice from Iugu's webhook",
response= Invoice.class,
notes="This Operation creates a subscription invoice from Iugu's webhook")
@PreAuthorize("#oauth2.hasScope('read')")
public ResponseEntity<Invoice> createSubscriptionInvoice(IuguWebhookDto iuguWebhookDto) {
try {
Invoice invoice = paymentService.createSubscriptionInvoiceFromIugusWebhook(iuguWebhookDto);
return new ResponseEntity<>(invoice, HttpStatus.CREATED);
} catch (EntityNotFoundException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage(), e);
} catch (Exception e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e);
}
}
IuguWebhookDto.java:
@Getter
@Setter
@NoArgsConstructor
@ToString
public class IuguWebhookDto implements Serializable {
private static final long serialVersionUID = -5557936429069206933L;
private String event;
private IuguDataDto data;
IuguDataDto.java:
@Getter
@Setter
@NoArgsConstructor
@ToString
public class IuguDataDto implements Serializable {
private static final long serialVersionUID = -5557936429069206933L;
private String id;
private String account_id;
private String status;
private String subscription_id;
如何在 Spring 引導中將這些請求參數作為 object 接收?
我在使用 Iugu Webhooks API 時遇到了同樣的問題。 為了解決這個問題,我只是對 Iugu 發送的原始數據進行字符串化,刪除不需要的字符,然后再次解析 object 以獲得我想要的變量。
var dataReceived = JSON.stringify(req.body).replace('data[id]','id').replace('data[status]','status').replace('data[account_id]','account_id');
var finalData = JSON.parse(dataReceived);
return res.status(200).send(finalData.id);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.