gpt4 book ai didi

java - 使用 Spring Data REST 的 JsonMappingException : Selects wrong serializer when @Id is removed from the list that that is being mapped

转载 作者:行者123 更新时间:2023-11-30 06:36:14 25 4
gpt4 key购买 nike

我创建了一个 project on gitlab(url)我想使用 Spring Data REST 来保存和公开一些传感器数据。当使用 HAL 浏览器进行测试时,持久化到 PostgreSQL DB 没有问题,但 SensorReading GET 给我带来了一些麻烦:

Could not write JSON: java.lang.Integer cannot be cast to java.lang.Double; nested exception is com.fasterxml.jackson.databind.JsonMappingException: java.lang.Integer cannot be cast to java.lang.Double (through reference chain: org.springframework.hateoas.Resources[\"_embedded\"]>java.util.Collections$UnmodifiableMap[\"sensorReadings\"]>java.util.ArrayList[0]>org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module$PersistentEntityResourceSerializer$1[\"content\"]->net.smurfz.kado.models.SensorReading[\"sensorId\"])

我的@Entity类中的代码(net.smurfz.kado.models.SensorReading):

@Entity
public class SensorReading {

@Id
@org.springframework.data.annotation.Id
@SequenceGenerator(name = "sensor_reading_id_seq",
sequenceName = "sensor_reading_id_seq",
allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator = "sensor_reading_id_seq")
@Column(updatable = false)
private Integer id;

@NotNull
private Integer sensorId;

@NotNull
private double reading;

@Column(name = "created_date")
@ReadOnlyProperty
private Date created;

public SensorReading() {
}

public SensorReading(Integer sensorId, double reading) {
this.sensorId = sensorId;
this.reading = reading;
}

public Integer getId() {
return id;
}

public Integer getSensorId() {
return sensorId;
}

public void setSensorId(Integer sensorId) {
this.sensorId = sensorId;
}

public double getReading() {
return reading;
}

public void setReading(double reading) {
this.reading = reading;
}

public Date getCreated() {
return created;
}

@PrePersist
protected void onCreate() {
created = new Date();
}
}

和我的@RepositoryRestResource类(net.smurfz.kado.repositories.SensorReadingRepository):

@RepositoryRestResource
public interface SensorReadingRepository extends CrudRepository<SensorReading, Integer> {
List<SensorReading> findTop30BySensorIdOrderByCreatedDesc(Integer sensorId);
Long countAllBySensorId(Integer sensorId);
SensorReading findFirstBySensorIdOrderByCreatedDesc(Integer sensorId);
Page<SensorReading> findAllBySensorId(Integer sensorId, Pageable pageable);
}

现在,我尊敬的同事设法找到的快速解决办法是移动:

        @Id
@org.springframework.data.annotation.Id
@SequenceGenerator(name = "sensor_reading_id_seq",
sequenceName = "sensor_reading_id_seq",
allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE,
generator = "sensor_reading_id_seq")
@Column(updatable = false)
private Integer id;

在所有其他字段声明下。

当 BeanSerializerFactory 类尝试定义要使用的序列化器时,会发生此错误。我的工作理论是,它最初识别 4 个字段(4 个列表),然后过滤掉不被序列化的 id(3 个列表),但决定使用第二个列表中第一个列表中的索引 (1-3) ,导致 Integer 字段获得下面定义的 double 字段的序列化器。

这是一个不需要的肮脏黑客,因为我真的不知道这可能会影响我想知道的其他事情:

a)为什么会发生这种情况?

b)应该采取什么不同的措施来避免此错误?

上面的 URL 直接链接到实现 id-fix 之前的提交。

最佳答案

引用@Damien 的评论:

I didnt no - based on this jira - github.com/spring-projects/spring-boot/issues/9756. Changing the version to 2.0.0.BUILD-SNAPSHOT resolves the issue. – Damien

springBootVersion = '2.0.0.BUILD-SNAPSHOT' 解决了这个问题。

关于java - 使用 Spring Data REST 的 JsonMappingException : Selects wrong serializer when @Id is removed from the list that that is being mapped,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45169166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com