尝试使用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时无法从请求主体获取对象的值[Node.js,MySQL]

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

使用ReadableStream作为请求主体获取

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

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

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

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

使用通用JSON对象作为请求主体

在POST请求主体Play框架中获取数据

使用节点获取时如何使请求主体类型与RequestInit或BodyInit兼容?

使用Groovy脚本在SoapUI中生成动态JSON请求主体并将其传递给POST API请求

尝试在Java SpringBoot中使用REST API时收到错误请求

如何使用REST请求主体方法执行ElasticSearch并获取返回的json格式/漂亮?

TOP 榜单

  1. 1

    来自Microsoft Office加载项taskpane.js的MySQL驱动程序模块的空引用

  2. 2

    使用AWS Cognito和React的仅限Facebook / Google的登录名(无用户名/密码)

  3. 3

    创建Windows Phone 8应用并将其连接到数据库的最佳方法(最好是SQL Server)

  4. 4

    为什么Java中的System.out.println()打印到控制台?

  5. 5

    卷曲函数无法解析来自bash中变量的代理

  6. 6

    是什么在Android的consumer-rules.pro和proguard-rules.pro之间的区别?

  7. 7

    设置与Apache POI Excel表散点图标记图标的颜色

  8. 8

    将Qt Pyside2与asyncio await语法一起使用?

  9. 9

    崇高的文字+蟒蛇的蟒蛇

  10. 10

    任务':app:minifyReleaseWithR8'.java.lang.NullPointerException的执行失败(无错误消息)

  11. 11

    OpenJDK的和AdoptOpenJDK的区别

  12. 12

    大型数据集缓存到Spark内存中时,“超出了GC开销限制”(通过sparklyr和RStudio)

  13. 13

    “执行测试CMAKE_HAVE_LIBC_PTHREAD”失败实际上是什么意思?

  14. 14

    使用Core 2.2中的Identity,如何在关闭浏览器15分钟后保持会话活动?

  15. 15

    React中的ForwardRefExoticComponent和ForwardRefRenderFunction有什么区别?

  16. 16

    猫鼬查找结果,然后将字段替换为findOne

  17. 17

    如何降级Google Colab的Torch版本

  18. 18

    Keras提前停止回调错误,val_loss指标不可用

  19. 19

    如何避免VSCode中的“导入路径不能以.ts扩展名结尾”错误?

  20. 20

    Nuxt.JS:如何在页面中获取路由URL参数

  21. 21

    是否有为什么会AccessibilityManager.sInstance导致内存泄漏的一个原因?

热门标签

归档