尝试使用springboot从POST请求主体读取嵌套JSON时获取null

SanthoshT:

我正在尝试使用springboot从请求主体解析JSON:

以下是邮递员的请求(请求类型为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
}

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

akortex91:

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

同样,也不需要这些不加任何值的复杂包装器(如果不包装,则使生成的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; }

}

CreditComplex:

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
    } ]
  } ]
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在执行POST请求并读取用户使用Spring Boot Rest API输入的POST数据时进行JSON模式验证

带有JSON主体的POST请求不会传递到springboot微服务架构中的端点

通过http请求获取嵌套Json返回null

尝试解析JSON时Spring RestTemplate响应主体为null

尝试读取JSON时在函数主体之外的非声明语句

如何从Go上的请求主体获取JSON

尝试使用Springs RestTemplate执行POST请求时获取RestClientException

尝试使用RestTemplate获取对象列表时响应主体为空

尝试读取属性文件Spring时获取NULL

使用ReadableStream作为请求主体获取

当尝试使用SpringBoot记录非GET请求的请求时,Jersey 2.x抛出NegativeArraySizeException

使用Karate框架发送嵌套JSON作为请求以验证POST REST API方法时出现问题

使用提取时,请求主体为空

使用请求主体的Scala Play框架POST请求

无法使用JSON主体复制POST请求

尝试使用数据进行POST时,Python请求获取TypeError

尝试POST时无法从请求主体获取对象的值[Node.js,MySQL]

如何在SprayTest中使用JSON主体模拟POST请求?

如何使用Nginx和Lua处理POST请求的JSON主体?

尝试发送POST请求并获取json数据,但得到400

使用SwiftyJSON从JSON读取数据时获取Null作为回报

使用HttpURLConnection执行POST请求时获取JSON响应

jQuery使用JSON主体获取对REST API的请求

使用 RxJS 从 JSON 获取数据尝试返回数组并将值传递给不同的行为主体

使用 cURL 和 PHP 发送嵌套的 json post 请求很热

使用嵌套 JSON 作为数据 + BLoC 模式的 Flutter Post 请求

尝试访问脚本时,使用 useEffect 在我的 React 项目中获取“TypeError:无法读取属性 'classList' of null”?

为 swift HTTP API 请求编码双嵌套 JSON 主体

尝试使用 Spring JPA 为带有外键的 JSON 对象发送 POST 请求时出现重复或空错误