如何将JSON体内的数组映射到Pojo



我正在使用Hibernate作为JPA和Spring-boot实施多一关系。当我从邮递员那里提出邮政请求时,我会收到以下错误:

Failed to convert from type [java.net.URI] to type [com.domain.Datasource]

MetricDatasource之间有许多与众不同的关系。

JSON主体

{
    "companyId" : "fake_company_id",
    "name" : "test_name",
    "description" :"test Description",
    "datasources" :  ["fdaea7d4162bd2c3f3db5ba059638123"]
}

metric.java

@Entity
public class Metric implements Persistable<Long> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
@RestResource(exported = false)
private Long id;
@Column(nullable = false, unique = true)
private String publicId;
@Column(nullable = false)
private String name;
private MetricType type;
@ManyToMany(cascade = CascadeType.ALL)
@JoinTable(name = "metric_datasource", joinColumns = @JoinColumn(name = "metric_id", referencedColumnName = "id"), inverseJoinColumns =  @JoinColumn(name = "datasource_id", referencedColumnName = "id"))
private Set<Datasource> datasources ;
private String definition;
private long dateCreated;
private String description;
private String companyId;
protected Metric() {
    this.dateCreated = new Date().getTime();
}
public Metric(String name, String publicId, String definition) {
    super();
    this.name = name;
    this.publicId = publicId;
    this.definition = definition;
}
@JsonIgnore
@Override
public boolean isNew() {
    return null == getId();
}
@PrePersist
public void generatePublicId() {
    publicId = PublicIdGenerator.generate(32);
}
@Override
public Long getId() {
    return id;
}
public String getPublicId() {
    return publicId;
}
public void setPublicId(String publicId) {
    this.publicId = publicId;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getDefinition() {
    return definition;
}
public void setDefinition(String definition) {
    this.definition = definition;
}
public void setDateCreated(long dateCreated) {
    this.dateCreated = dateCreated;
}
public long getDateCreated(){
    return this.dateCreated;
}
public String getDescription() {
    return description;
}
public void setDescription(String description) {
    this.description = description;
}
public Set<Datasource> getDatasources() {
    if (datasources != null) {
        return datasources;
    } else {
        return Collections.emptySet();
    }
}
public void setDatasources(Set<Datasource> datasources) {
    if (datasources != null) {
        this.datasources = datasources;
    } else {
        this.datasources = Collections.emptySet();
    }
}
public void setId(Long id) {
    this.id = id;
}
public String getCompanyId() {
    return companyId;
}
public void setCompanyId(String companyId) {
    this.companyId = companyId;
}
public MetricType getType() {
    return type;
}
public void setType(MetricType type) {
    this.type = type;
}
public void update(Metric metric) {
    if (metric.getName() != null) {
        setName(metric.getName());
    }
    if (metric.getDescription() != null) {
        setDescription(metric.getDescription());
    }
    if (metric.getDefinition() != null) {
        setDefinition(metric.getDefinition());
    }
}

datasource.java

@Entity
public class Datasource implements Persistable<Long> {
@Column(nullable = false, unique = true)
private String publicId;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(nullable = false)
@RestResource(exported = false)
private Long id;
@ManyToMany(mappedBy = "datasources")
private Set<Metric> metrics;
public Datasource() {
    this(null);
}
public Datasource(String publicId) {
    this.publicId = publicId;
}
public String getPublicId() {
    return publicId;
}
public void setPublicId(String publicId) {
    this.publicId = publicId;
}
@Override
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
@Override
public boolean isNew() {
    return null == getId();
}
public Set<Metric> getMetrics() {
    return metrics;
}
public void setMetrics(Set<Metric> metrics) {
    this.metrics = metrics;
}

当前,公制和数据源之间的关系已正确设置,即我有三个表metricdatasourcemetric_datasource在数据库中创建的。但是我不确定如何使用给予JSON主体保存值。请帮助。

首先,请确保您的ret-controller中的方法可以消耗"应用程序/json",在其参数中具有适当的@RequestBody注释。
其次,我认为从客户端发送类似域的域名对象是不正确的。您应该为Metric类实现DTO,该类别将完全匹配您提供的JSON。datasources属性应为List<String>,其中包含在这种情况下的实际数据源ID。

然后,您应该在您的方法中实现一种将DTO转换为域对象的方法(您可以在DTO中获取每个DataSource ID的DataSource对象(然后坚持下去。

// assuming that you already implemented any JpaRepository<Metric> interface
@Autowired
private MetricRepository metricRepo;
@Autowired
private DataSourceRepository dataSourceRepo;
public Metric saveMetricDTO(MetricDTO dto) {
  Metric metric = metricRepo.findByName(dto.getName()); // or fetch by any other suitable property
  // assign all props from DTO to metric: metric.setSmth(dto.getSmth())
  // ...
  List<DataSource> dsList = dto.getDataSourceIds().stream()
    .map(dataSourceRepo::findOne)
    .collect(Collectors.toList())
  metric.setDataSources(dsList);
  metricRepo.save(metric); // finally persist object
}

相关内容

  • 没有找到相关文章

最新更新