gpt4 book ai didi

java - 尝试使用 Spring Boot 从 POST 请求正文读取嵌套 JSON 时获取 null

转载 作者:行者123 更新时间:2023-12-02 01:07:34 26 4
gpt4 key购买 nike

我正在尝试使用 springboot 从请求正文中解析 JSON:

下面是来自 postman 的请求(请求正文,类型为 application/json)->

{
"CreditComplex": [{
"BCNR":"973",
"RelationshipName":"Pricing Company BV",
"Borrowers":[{
"Borrower ":[{
"BCNR" : "",
"CGC":"1174",
"UCR":"4+",
"PD":"0.03",
"AGIC":"1301",
"BookingLocation":"NL",
"SalesTurnover":"10000000000",
"Facilities": [{
"Facility": [{
"gfid " : "",
"limit": "1000000",
"producttype": "19",
"spread": "3.0000",
"commitmentFee": "0.3000",
"annualfee": "0",
"oneOfffee": "5000",
"lgd": "0.1800",
"outstanding": "500000"
}]
}]
}]
}]
}]}

Post方法处理来自springboot的请求

   @RequestMapping(path = "/checkjson", method = RequestMethod.POST)
public ResponseEntity<CreditComplex> getBook(@RequestBody CreditComplex creditcomplex) {
return new ResponseEntity<>(creditcomplex, HttpStatus.OK);
}

我创建了以下类来映射 JSON 中的值:

    public class CreditComplex {

private String BCNR;
private String RelationshipName;
List<Borrower> borrower;

所有字段都有 getter 和 setter

public class Borrowers {

List <Borrower> borrower;

所有字段都有 getter 和 setter

public class Borrower {

private String BCNR;
private String CGC;
private String UCR;
private String PD;
private String AGIC;
private String BookingLocation;
private String SalesTurnover;
private String Spread;
private String CommitmentFee;
private String AnnualFee;
private String OneOffFee;
private String LGD;
private String Outstanding;
List<Facilities> facilities;

所有字段都有 getter 和 setter

public class Facilities {

private List<Facility> facility;

所有字段都有 getter 和 setter

public class Facility {

private String gfid;
private String limit;
private String producttype;
private String spread;
private String commitmentfee;
private String annualfee;
private String oneOfffee;
private String lgd;
private String outstanding;
with getter and setter for all fields

预期响应是已映射到 Credit 复杂类的实际请求 json,但得到的响应如下:

{
"borrower": null,
"bcnr": null,
"relationshipName": null
}

任何发现/帮助将不胜感激。

最佳答案

您的 JSON 架构错误。预期字段和 JSON 请求正文之间存在命名差异。

此外,也没有必要使用这些没有任何值(value)的复杂包装器(如果不会使生成的 JSON 更难以阅读)。只需将您的代码重构为如下所示:

public class Facility {

private String gfid;
public String getGfid() { return gfid; }
public void setGfid(String gfid) { this.gfid = gfid; }

private Integer limit;
public Integer getLimit() { return limit; }
public void setLimit(Integer limit) { this.limit = limit; }

private Integer productType;
public Integer getProductType() { return productType; }
public void setProductType(Integer productType) { this.productType = productType; }

private Double spread;
public Double getSpread() { return spread; }
public void setSpread(Double spread) { this.spread = spread; }

private Double commitmentFee;
public Double getCommitmentFee() { return commitmentFee; }
public void setCommitmentFee(Double commitmentFee) { this.commitmentFee = commitmentFee; }

private Double annualFee;
public Double getAnnualFee() { return annualFee; }
public void setAnnualFee(Double annualFee) { this.annualFee = annualFee; }

private Double oneOffFee;
public Double getOneOffFee() { return oneOffFee; }
public void setOneOffFee(Double oneOffFee) { this.oneOffFee = oneOffFee; }

private Double lgd;
public Double getLgd() { return lgd; }
public void setLgd(Double lgd) { this.lgd = lgd; }

private Integer outstanding;
public Integer getOutstanding() { return outstanding; }
public void setOutstanding(Integer outstanding) { this.outstanding = outstanding; }

}

借款人:

public class Borrower {

private String bncr;
public String getBncr() { return bncr; }
public void setBncr(String bncr) { this.bncr = bncr; }

private Integer cgc;
public Integer getCgc() { return cgc; }
public void setCgc(Integer cgc) { this.cgc = cgc; }

private String ucr;
public String getUcr() { return ucr; }
public void setUcr(String ucr) { this.ucr = ucr; }

private Double pd;
public Double getPd() { return pd; }
public void setPd(Double pd) { this.pd = pd; }

private Integer agic;
public Integer getAgic() { return agic; }
public void setAgic(Integer agic) { this.agic = agic; }

private String bookingLocation;
public String getBookingLocation() { return bookingLocation; }
public void setBookingLocation(String bookingLocation) { this.bookingLocation = bookingLocation; }

private Integer salesTurnover;
public Integer getSalesTurnover() { return salesTurnover; }
public void setSalesTurnover(Integer salesTurnover) { this.salesTurnover = salesTurnover; }

private List<Facility> faciliies;
public List<Facility> getFaciliies() { return faciliies; }
public void setFaciliies(List<Facility> faciliies) { this.faciliies = faciliies; }

}

信用综合体:

public class CreditComplex {

private String bcnr;
public String getBcnr() { return bcnr; }
public void setBcnr(String bcnr) { this.bcnr = bcnr; }

private String relationshipName;
public String getRelationshipName() { return relationshipName; }
public void setRelationshipName(String relationshipName) { this.relationshipName = relationshipName; }

private List<Borrower> borrowers;
public List<Borrower> getBorrowers() { return borrowers; }
public void setBorrowers(List<Borrower> borrowers) { this.borrowers = borrowers; }

}

将其传递到对象映射器将生成以下 JSON 正文:

{
"bcnr" : "973",
"relationshipName" : "Pricing Company BV",
"borrowers" : [ {
"bncr" : "",
"cgc" : 1174,
"ucr" : "4+",
"pd" : 0.03,
"agic" : 1301,
"bookingLocation" : "NL",
"salesTurnover" : 10000000,
"faciliies" : [ {
"gfid" : "",
"limit" : 10000,
"productType" : 19,
"spread" : 3.0,
"commitmentFee" : 0.3,
"annualFee" : 0.0,
"oneOffFee" : 5000.0,
"lgd" : 0.18,
"outstanding" : 50000
} ]
} ]
}

关于java - 尝试使用 Spring Boot 从 POST 请求正文读取嵌套 JSON 时获取 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59821237/

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