当前位置: 首页 > 知识库问答 >
问题:

数据发布后json解析错误spring boot mysql

濮阳浩穰
2023-03-14

我在后端有这个问题,当我发布一个数据形式从角请有人帮我我尝试解决这个问题的对象映射器,但我不明白如何才能正确地做它错误是:

  • 已解析[org.springframework.http.converter.httpMessageNodeAbleException:JSON解析错误:无法构造实体.Group的实例(尽管至少存在一个创建者):没有字符串参数构造函数/工厂方法从字符串值反序列化(“{”idGrp“:2,“nomGroup”:“intranet”}”); 嵌套的异常是com。fasterxml。杰克逊。数据绑定。exc.MismatchedInputException:无法构造实体的实例。Group(尽管至少存在一个创建者):在[Source:(PushbackInputStream);line:1,column:236](通过引用链:entity.Employe[“Group”])没有字符串参数构造函数/工厂方法从字符串值(“{”idGrp“:2,“nomGroup:”intranet“}”)反序列化*

这是我的班级小组

package entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="groups")
public class Group implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long idGrp;
    
    private String NomGroup;
    
    public Group()
    {
        
    }

    public Group(Long id, String nomGroup) {
        super();
        this.idGrp = id;
        NomGroup = nomGroup;
    }


}

我试图发布一个包含实体组类employee数据的employee数据

package entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.GenericGenerator;

@Entity
public class Employe  implements Serializable {
     @Id
     @GeneratedValue(strategy = GenerationType.AUTO, generator = "myid")
     @GenericGenerator(name = "myid", strategy = "entity.MyGenerator")
     private Integer matricule;

    private String  nom;
    
    private String   prenom ;

     private String  nom_ar;
    
     private String prenom_ar;

     
     private int age;
    
     private String description;

     private String email;

     private int codeP;

     private int numTele;

     private String adresse;

     private String  ville;
     @JoinColumn(name="idGrp")
     @ManyToOne(cascade = {CascadeType.ALL})     
    Group group;
     
     
    
    public Employe(Integer matricule, String nom, String prenom, String nom_ar, String prenom_ar, int age,
            String description, String email, int codeP, int numTele, String adresse, String ville, Group group) {
        super();
        this.matricule = matricule;
        this.nom = nom;
        this.prenom = prenom;
        this.nom_ar = nom_ar;
        this.prenom_ar = prenom_ar;
        this.age = age;
        this.description = description;
        this.email = email;
        this.codeP = codeP;
        this.numTele = numTele;
        this.adresse = adresse;
        this.ville = ville;
    
    }
    public Employe(Integer matricule, String nom, String prenom, String nom_ar, String prenom_ar, int age,
            String description, String email, int codeP, int numTele, String adresse, String ville) {
        
        this.matricule = matricule;
        this.nom = nom;
        this.prenom = prenom;
        this.nom_ar = nom_ar;
        this.prenom_ar = prenom_ar;
        this.age = age;
        this.description = description;
        this.email = email;
        this.codeP = codeP;
        this.numTele = numTele;
        this.adresse = adresse;
        this.ville = ville;
    }


}

控制

 @CrossOrigin(origins = "http://localhost:4200")
    
    @PostMapping("addEmp")
    public ResponseEntity<Employe> addEmp(@RequestBody Employe ep) throws JsonProcessingException
    {
        Employe p=this.servEmp.addEmp(ep);
    
    return new ResponseEntity<>(p, HttpStatus.CREATED);
        }

请帮助我如何将json数据转换为对象我现在花了两天在这个错误

I try to use commandRunner 
    @Bean
       public CommandLineRunner run(ServiceEmp ServiceEmp) {
        
        return args->{
            
            Employe e=new Employe(0,"","","","",0,"","",0,0,"","");
            ObjectMapper objectMapper=new ObjectMapper();
            TypeReference<Employe> TypeReference =new TypeReference<Employe>() {};
            InputStream InputStream=TypeReference.class.getResourceAsStream(objectMapper.writeValueAsString(ServiceEmp.addEmp(e)));
            try {
                    e=objectMapper.readValue(InputStream,TypeReference);
            ServiceEmp.addEmp(e);
            System.out.print("saved");
            
            }
            catch(IOException a)
            {
                System.out.print("not saved"+a.getMessage());
                
            }

输出:

**Employe [nom=, prenom=, nom_ar=, prenom_ar=, matricule=0, age=0, description=, email=, codeP=0, numTele=0, adresse=, ville=]
[2m2021-09-25 20:40:39.966[0;39m [32m INFO[0;39m [35m10044[0;39m [2m---[0;39m [2m[           main][0;39m [36mConditionEvaluationReportLoggingListener[0;39m [2m:[0;39m 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
[2m2021-09-25 20:40:39.992[0;39m [31mERROR[0;39m [35m10044[0;39m [2m---[0;39m [2m[           main][0;39m [36mo.s.boot.SpringApplication              [0;39m [2m:[0;39m Application run failed

java.lang.IllegalStateException: Failed to execute CommandLineRunner
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:794) ~[spring-boot-2.5.4.jar:2.5.4]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:775) ~[spring-boot-2.5.4.jar:2.5.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:345) ~[spring-boot-2.5.4.jar:2.5.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1343) ~[spring-boot-2.5.4.jar:2.5.4]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1332) ~[spring-boot-2.5.4.jar:2.5.4]
    at cni.tn.CniApp.CniAppApplication.main(CniAppApplication.java:34) ~[classes/:na]
Caused by: java.lang.IllegalArgumentException: argument "src" is null
    at com.fasterxml.jackson.databind.ObjectMapper._assertNotNull(ObjectMapper.java:4693) ~[jackson-databind-2.11.1.jar:2.11.1]
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3478) ~[jackson-databind-2.11.1.jar:2.11.1]
    at cni.tn.CniApp.CniAppApplication.lambda$0(CniAppApplication.java:46) ~[classes/:na]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:791) ~[spring-boot-2.5.4.jar:2.5.4]
    ... 5 common frames omitted**

共有1个答案

公羊英达
2023-03-14

问题是带参数的构造函数的用法。默认情况下,杰克逊无法映射此。我认为,有两个选择:

  1. 添加默认构造函数和所有必需的getter和setter
  2. 将@JsonCreator添加到参数化构造函数中

例如:

public class Bean {
    public int id;
    public String name;

    @JsonCreator
    public Bean(
      @JsonProperty("id") int id, 
      @JsonProperty("theName") String name) {
        this.id = id;
        this.name = name;
    }
}

我不确定,如果你真的需要@JsonProperty作为构造函数的参数,如果你使用的是Spring引导!

 类似资料:
  • 我有一个实体User,它有两个Data类型的字段。 我有一个带有save()方法的Controller,它将用户的数据保存到postgesql数据库中。 我有一个JSON,我将它发送到controller { "name":"Max "," createdOn":"2019-07-26 11:13:39 "," modified on ":" 2019-07-26 11:13:39 " } 最后在

  • 问题内容: 尝试对mongoexport使用查询会导致错误。但是mongo-client会评估相同的查询,而不会出错。 在mongo-client中: 使用mongoexport: 产生的错误: 但是预先在mongoexport中进行乘法: 作品! 为什么mongo在这两种情况下对查询的评估不同? 问题答案: 该命令行实用程序支持传递查询 JSON 格式,但您要评估 的JavaScript 在您的

  • 和 是我用来获取JSON并对其进行解析的类。但是当我运行第一个时,它会报告以下堆栈跟踪: 它告诉我添加JsonReader。将lenient(true)设置为我的代码,但我的代码不使用JsonReader。那么如何将setLenient(true)添加到代码中? 编辑:添加API响应(格式化):

  • 我有以下命令。我想循环使用此键和值,即对于冰/冷的项目,打印“值” 我试过这个: 却给我错误

  • 我一直得到一个JSON解析器错误,每次我尝试连接从Android到php帐户。下面你会看到我得到的错误: 谁能帮帮我吗。。。 PHP代码: Android代码: Android代码: 公共JSONObject getJSONFromUrl(字符串url,列表参数){ 注册用户JSONObject PhP警告: php代码: 链接89是$no_of_rows 用户表结构: mysql_error:

  • 问题内容: 解析JSON之后,我将数据保存在ViewController的arraOfData中,但collectionView不会显示此数据。如果我使用静态数组,一切正常。collectionview.reloadData()无法正常工作。我认为,需要以某种方式等待解析的数据,然后尝试加载collectionView。我尝试使用refreshControler来等待数据,但是我不知道put方法“