Home > Software design >  JSON parse error: Cannot construct instance of 'com.example.application" (although at leas
JSON parse error: Cannot construct instance of 'com.example.application" (although at leas

Time:03-10

I'm trying to persiste to MySQL database ith POST method and I'm getting this error

  • I'm using Swagger to generate request Body
  • I'm trying to make OneToMany and ManyToOne
  • I'm using spring boot with maven multi-modules.

{ "timestamp": "2022-03-08T20:59:23.477 0000", "status": 400,
"error": "Bad Request", "message": "JSON parse error: Cannot construct instance of releve.domain.domain.ReleveBancaire (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('string'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of releve.domain.domain.ReleveBancaire (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('string')\n at [Source: (PushbackInputStream); line: 22, column: 25] (through reference chain: releve.domain.domain.ReleveBancaire["lignereleve"]->java.util.ArrayList[0]->releve.domain.domain.LigneReleve["releveBancaire"])", "path": "/api/relevebancaire" }

Entities:

@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@Table(name = "RELEVEBANCAIRE")
@Entity
public class ReleveBancaireEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, updatable = false)
    private Long releveBancaireId;
    private Date dateReception;
    private String label;
    private int nbrLignes;
    private int nbrOperationCredit;
    private int nbrOperationDebit;
    private BigDecimal soldeInitial;
    private BigDecimal soleFinal;
    @OneToMany(mappedBy = "releveBancaire", cascade = CascadeType.ALL, fetch = FetchType.LAZY, orphanRemoval = true)
    @JacksonXmlProperty(localName = "lignereleve")
    @JacksonXmlElementWrapper(useWrapping = false)
    private List<LigneReleveEntity> lignereleve;

===

@Table(name = "LIGNERELEVE")
@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class LigneReleveEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(nullable = false, updatable = false)
    private Long ligneReleveId;
    private Date dateOperation;
    private String operationNature;
    private String rib;
    private int numCheck;
    private BigDecimal montant;
    private BigDecimal creditDebit;
    private int refDER;
    private int refPaiment;
    private String modePaiment;
    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "idReleve")
    private ReleveBancaireEntity releveBancaire;

This is my request body:

{
  "releveBancaireId": 0,
  "dateReception": "2022-03-08T20:59:22.026Z",
  "label": "string",
  "nbrLignes": 0,
  "nbrOperationCredit": 0,
  "nbrOperationDebit": 0,
  "soldeInitial": 0,
  "soleFinal": 0,
  "lignereleve": [
    {
      "ligneReleveId": 0,
      "dateOperation": "2022-03-08T20:59:22.027Z",
      "operationNature": "string",
      "rib": "string",
      "numCheck": 0,
      "montant": 0,
      "creditDebit": 0,
      "refDER": 0,
      "refPaiment": 0,
      "modePaiment": "string",
      "releveBancaire": "string",    **<========= this line**
      "operationCredit": {
        "operationCreditId": 0,
        "operationDate": "2022-03-08T20:59:22.027Z",
        "operationCheque": {
          "numeroCheque": 0,
          "cheque": {
            "numerCheque": 0,
            "acteur": {
              "acteurId": 0,
              "nomActeur": "string",
              "prenomActeur": "string"
            }
          }
        },
        "operationEspeces": {
          "cin": "string",
          "nomEmetteur": "string",
          "prenomEmetteur": "string"
        },
        "operationVirement": {
          "rib": "string",
          "compteBancaire": {
            "compteBancaireId": 0,
            "rib": "string",
            "codeSwift": "string",
            "banque": {
              "banqueId": 0,
              "nomBanque": "string"
            },
            "acteur": {
              "acteurId": 0,
              "nomActeur": "string",
              "prenomActeur": "string"
            }
          }
        },
        "produit": {
          "produitId": 0,
          "produitCode": "string",
          "produitLabel": "string"
        }
      }
    }
  ]
}

So the problem is that i'm having One ReleveBancaireEntity in LigneReleveEntity but i's giving me ReleveBancaireEntity as a String I tried to implement NoArg Constructor for deserialize with no result, I'm using Swagger for posting it, Swagger generates format for me. Please any help I'll be appreciate

CodePudding user response:

Check if your constructor is actually public? Also try running with “ignore on unknown properties” once to see the constructed object from json.

CodePudding user response:

I tried the exact code (copy pasted), and it just worked. I have uploaded it on github for you to be able to refer https://github.com/srjha/stackoverflow-71386316

  • Related