【问题标题】:springboot: writing array of object to dbspring boot:将对象数组写入数据库
【发布时间】:2019-12-10 07:10:40
【问题描述】:

对单表感兴趣


下面是我要存储在数据库中的JSON 文件。我应该如何编写模型类?尝试了一个示例模型,但它只存储对象的大小,而不是实际数据。我也无法检索数据。
{
 sourceImageRepo: "xxx",
 sourceTagMatch: "xxx",
 targetProject: "xxx",
 targetFiles: [
  {
      name: "xxx",
      pattern: "xxx"
  },
  {
      name: "xxx",
      pattern: "xxx"
  }
 ]
}

我收到如下错误消息:

{
    "timestamp": "2019-12-10T06:36:42.745+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "Could not write JSON: (was java.lang.ArrayIndexOutOfBoundsException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.ArrayIndexOutOfBoundsException) (through reference chain: org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer$1[\"content\"]->com.example.demo.model.suiteInformations[\"targetFiles\"]->java.util.ArrayList[0])",
    "path": "/suiteInformationses/1"
} 

当前型号

suiteInformations.java

package com.example.demo.model;

import javax.persistence.*;
import java.util.ArrayList;

@Entity
@Table(name = "suitesInformations")
public class suiteInformations {
    public suiteInformations(int id, String sourceImageRepo, String sourceTagMatch, String email, 
       String targetProject, ArrayList<TargetFiles> targetFiles) {
        this.id = id;
        this.sourceImageRepo = sourceImageRepo;
        this.sourceTagMatch = sourceTagMatch;
        Email = email;
        this.targetProject = targetProject;
        this.targetFiles = targetFiles;
    }

    public suiteInformations() {}

    @Id
    @Column(name = "id")
    @GeneratedValue
    public int id;
    @Column(name = "sourceImageRepo")
    public String sourceImageRepo;
    @Column(name = "sourceTagMatch")
    public String sourceTagMatch;
    @Column(name = "Email")
    public String Email;
    @Column(name = "targetProject")
    public String targetProject;
    @Embedded
    public ArrayList<TargetFiles>  targetFiles;

    // getters/setters omitted

}

TargetFiles.java

package com.example.demo.model;

import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.util.ArrayList;

@Embeddable
public class TargetFiles {
    @Column(name = "name")
    public String name;

    @Column(name = "pattern")
    public String pattern;
}

数据库内容

SELECT * FROM SUITESINFORMATIONS;
ID EMAIL  SOURCEIMAGEREPO SOURCETAGMATCH    SIZE     TARGETPROJECT
1   xxx    xxx               xxx            1          xxx

【问题讨论】:

  • 您使用的是哪个数据库?另外,像列数据类型一样指定表 DDL。
  • 我正在使用内部 h2 db 来测试模型

标签: java database spring spring-boot serialization


【解决方案1】:

我会使用 Spring 的 spring-boot-starter-data-jpa 依赖项。查看 Spring 中的 @Repository 注释也可能对您有所帮助。

spring-boot-starter-data-jpa的示例教程:https://spring.io/guides/gs/accessing-data-jpa/

您的JSON 文件可能成为 POJO 类:

@Entity
public class SuiteInfo {

   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   private Long id;

   private String sourceImageRepo;
   private String sourceTagMatch;
   private String targetProject;
   private List<SuiteFile> targetFiles;

   //relevant getters, setters, equals, etc

   public class SuiteFile {
      private String name;
      private String pattern;

      //relevant getters, setters, equals, etc
   }
}

然后可以创建存储库接口:

import org.springframework.data.repository.CrudRepository;

public interface SuiteInfoRepository extends CrudRepository<SuiteInfo, Long> {

  SuiteInfo findById(long id);

  // define other queries here
}

注意:(来自:https://spring.io/guides/gs/accessing-data-jpa/You need not write an implementation of the repository interface. Spring Data JPA creates an implementation when you run the application.

【讨论】:

  • 无法确定类型:java.util.List,表:suite_info,列:[org.hibernate.mapping.Column(target_files)]
  • @PoojithKumarHegde, java.util.List 导入错误,已被删除
【解决方案2】:

您在 json 中缺少键的引号。我希望这只是一个错字。

{
 "sourceImageRepo": "xxx",
 "sourceTagMatch": "xxx",
 "targetProject": "xxx",
 "targetFiles": [
  {
      "name": "xxx",
      "pattern": "xxx"
  },
  {
      "name": "xxx",
      "pattern": "xxx"
  }
 ]
}

您如何尝试将 json 映射到对象中? 它是 api 的请求正文吗?能否请您提供该信息。

【讨论】:

  • 这是 api 的请求正文,我对单个表感兴趣。
猜你喜欢
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-02
  • 1970-01-01
  • 2014-12-16
  • 1970-01-01
  • 2020-12-29
相关资源
最近更新 更多