- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了一个 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/
我以为我有一个父类参数,它有2个子类 ComboParameter 和 IntegerParameter @JsonSubTypes({ @JsonSubTypes.Type(value =
运行以下行时: Request> requestMap = JsonUtils.fromJson(eventContext.getMessage().getPayloadAsString(), Req
我已经使用 jersey 在 java 中编写了一个 REST API。当我使用 XML 请求时,一切正常,但当我使用 JSON 请求时,我总是得到 JsonMappingException。 这些对
我使用spring RestTemplate并将json响应转换为pojo。但得到一个 JsonMappingException POJO @Root public class Account { @
我的此类带有一个标有 @JsonCreator 的构造函数,用于将 JSON 字符串反序列化到我的类 Meal 中: public class Meal { private int prote
我的 IDE 在 mapper.readValue 行给我错误 Unhandled Exception com.fasterxml.jackson.databind.JsonMappingExcept
我在 Java 中有一个通用类型的类。它应该在收到一些 json 后返回一个 T 类型的对象。我正在使用以下代码创建对象: ObjectMapper mapper = new ObjectMapper
我有一个用 Java (spring boot) 编写的 rest api,我的请求从请求 header 中获取一个 json 字符串(不要问为什么这样:),例如{flowerId: 123}在我的
我正在尝试向我的网页显示数据库数据。当 GET 请求到 @RequestMapping(value = "/api/binder") 时,我做了以下代码。 但是当获取请求到达此方法时,它会获取数据(我
我正在构建一个使用并返回 JSON 的 RESTful Web 服务。当我尝试通过服务层从数据库中获取 ESRBRating 对象时,我遇到了以下堆栈跟踪。但是,当我将 Spring Data JPA
我在 Camel Route 中遇到了异常 Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer
对传递给 ObjectMapper Json 解析器方法的字符串进行 null 和空检查时,出现异常。这是我的代码: private static final ObjectMapper OBJECT_
我正在使用 Retrofit 2 和 jackson 转换器下载并解析此 json `{ "message": "Action complete.", "successCode": 0,
我知道关于我会告诉你的同一个问题有很多问题,但还没有一个能解决我的问题。 我已经实现了一项服务,该服务必须通过延迟加载来访问对象属性。这是类的简化: @Entity public class Trea
我有一个类需要使用 Jackson 从 JSON 反序列化。类结构如下所示: public class A { public B b; } public class B { publi
我尝试反序列化包含空属性并具有 JsonMappingException 的对象。 我的工作: String actual = "{\"@class\" : \"PersonResponse\","
尝试反序列化 JSON 时遇到以下异常 No suitable constructor found for type [simple type, class MyObj$obj$Card]: can
我有一个 Web 应用程序,其中服务器以 JSON 格式返回任何结果。为了创建 JSON,我使用 codehaus Jackson ObjectWriter 版本 1.9.8。 我遇到的问题是映射中有
我在尝试放置“可选实体过滤”时遇到问题。我有一个抽象类,如下所示: // In your Pom org.glassfish.jersey.ext je
尝试使用 @JsonIdentityInfo jackson 注释时出错。当我尝试反序列化对象时,出现以下异常: Could not read JSON: Already had POJO for i
我是一名优秀的程序员,十分优秀!