SanthoshT
SanthoshT

Reputation: 1

Getting null when trying to read nested JSON from POST request body using springboot

I am trying to Parse a JSON from request body using springboot :

Below is the Request from postman (request body with type as 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 method handle the request from springboot

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

I have created the following classes to map the values from JSON :

    public class CreditComplex {

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

with getter and setter for all fields

public class Borrowers {

    List <Borrower> borrower;

with getter and setter for all fields

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;

with getter and setter for all fields

public class Facilities {

    private List<Facility> facility;

with getter and setter for all fields

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

Response expected is the actual request json that has been mapped to Credit complex class but getting the response as below :

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

Any findings/ help would be appreciated.

Upvotes: 0

Views: 2001

Answers (1)

akourt
akourt

Reputation: 5563

Your JSON schema is wrong. There are naming differences between the expected fields and the JSON request body.

Also there is no need to have these convoluted wrappers that add no value (if not make the produced JSON even harder to read). Just refactor your code to look like this:

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

}

Borrower:

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

}

Passing this into an object mapper will produce the following JSON body:

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

Upvotes: 2

Related Questions