Java 为什么使用quarkus华丽、hibernate和jsonb的反序列化失败了?
我想玩一下hibernate、panache和quarkus。我想构建一个“跟踪”api,其中一个人可以有几个跟踪,但一个跟踪只能由一个人拥有。但我面临一个反序列化错误 假设我想发布以下内容:Java 为什么使用quarkus华丽、hibernate和jsonb的反序列化失败了?,java,hibernate,jsonb,quarkus,quarkus-panache,Java,Hibernate,Jsonb,Quarkus,Quarkus Panache,我想玩一下hibernate、panache和quarkus。我想构建一个“跟踪”api,其中一个人可以有几个跟踪,但一个跟踪只能由一个人拥有。但我面临一个反序列化错误 假设我想发布以下内容: curl --location --request POST 'localhost:8080/trace' \ --header 'Content-Type: application/json' \ --data-raw '{ "traceOwner": "Atest
curl --location --request POST 'localhost:8080/trace' \
--header 'Content-Type: application/json' \
--data-raw '{
"traceOwner": "AtestOwner",
"participants": ["Karl", "John"],
"place": "10101, TestCity, TestStreet 25",
"startTime": "2016-07-20T23:07:41.907+02:00[Europe/Berlin]",
"stopTime": "2016-07-27T23:07:45.807+02:00",
"comment": "TestComment"
}'
我得到以下错误:
javax.ws.rs.ProcessingException: RESTEASY008200: JSON Binding deserialization error: javax.json.bind.JsonbException:
Unable to deserialize property 'traceOwner' because of: Error deserialize JSON value into type: class
de.test.Person.
在日志中:
2020-07-27 10:48:12,597 SEVERE [org.ecl.yas.int.Unmarshaller] (executor-thread-1) Unable to deserialize property 'traceOwner' because of: Error deserialize JSON value into type: class de.test.Person.
我的实体如下所示:
import java.time.ZonedDateTime;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
@Entity
public class Trace extends PanacheEntity {
@ManyToOne
public Person traceOwner;
@ElementCollection
public List<String> participants;
public String place;
@Column(columnDefinition = "TIMESTAMP WITH TIME ZONE")
public ZonedDateTime startTime;
@Column(columnDefinition = "TIMESTAMP WITH TIME ZONE")
public ZonedDateTime stopTime;
public String comment;
public String toString() {
return String.format("%s, [%s, %s], %s, %s, %s", this.traceOwner, this.participants.get(0),
this.participants.get(1), this.place, this.startTime, this.stopTime, this.comment);
}
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createTrace(@Valid Trace trace) {
try {
LOG.info(trace);
transaction.begin();
trace.persist();
transaction.commit();
} catch (NotSupportedException | SystemException | SecurityException | IllegalStateException | RollbackException
| HeuristicMixedException | HeuristicRollbackException e1) {
LOG.error("Could not finish transaction to save entity", e1);
return Response.status(HttpStatus.SC_BAD_REQUEST).build();
}
return Response.ok(Trace.find("startTime =?1 AND traceOwner = ?2", trace.startTime, trace.traceOwner).firstResult())
.build();
}
POST端点如下所示:
import java.time.ZonedDateTime;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.ManyToOne;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
@Entity
public class Trace extends PanacheEntity {
@ManyToOne
public Person traceOwner;
@ElementCollection
public List<String> participants;
public String place;
@Column(columnDefinition = "TIMESTAMP WITH TIME ZONE")
public ZonedDateTime startTime;
@Column(columnDefinition = "TIMESTAMP WITH TIME ZONE")
public ZonedDateTime stopTime;
public String comment;
public String toString() {
return String.format("%s, [%s, %s], %s, %s, %s", this.traceOwner, this.participants.get(0),
this.participants.get(1), this.place, this.startTime, this.stopTime, this.comment);
}
}
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response createTrace(@Valid Trace trace) {
try {
LOG.info(trace);
transaction.begin();
trace.persist();
transaction.commit();
} catch (NotSupportedException | SystemException | SecurityException | IllegalStateException | RollbackException
| HeuristicMixedException | HeuristicRollbackException e1) {
LOG.error("Could not finish transaction to save entity", e1);
return Response.status(HttpStatus.SC_BAD_REQUEST).build();
}
return Response.ok(Trace.find("startTime =?1 AND traceOwner = ?2", trace.startTime, trace.traceOwner).firstResult())
.build();
}
为了避免缓存问题,我打开了一个新事务,但我想这不是问题所在吧
我真的不明白为什么jsonB会抱怨反序列化以及我如何修复它。另外,在某个时刻,我希望
公共列表参与者
成为公共列表参与者
,但这可以等到关系和反序列化工作开始。因此,我发现我做错了什么。两件事:
@ManyToOne(cascade=CascadeType.ALL)
也可以在持久化时更新瞬态属性你能粘贴错误信息和stacktrace以便我们更好地了解问题吗?我附上了错误信息以及我可以从日志中收集到的信息。即使我使用
/gradlew quarkusDev--stacktrace
或,日志看起来也是一样的--debug
公平地说,我认为这不起作用的原因与quarkus无关(尽管我想知道如何更好地调试错误),而是我犯了一些与hibernate相关的错误