Issue
I wanted to play around with hibernate, panache and quarkus a bit. I wanted to build a "tracing" api where a person can have several traces but a trace can only be owned by one person. but I am facing a deserialization error.
Let's say I want to post this:
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"
}'
I get the following errors:
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.
and in the logs:
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.
My Entities look like this:
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);
}
}
and
import javax.persistence.Entity;
import io.quarkus.hibernate.orm.panache.PanacheEntity;
@Entity
public class Person extends PanacheEntity {
public String name;
}
The POST endpoint looks like this:
@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();
}
To avoid a caching problem I open a new transaction, but I guess that is not the problem?
I don't really get why jsonB complains about the deserialization and how I can fix it. Also, at some point I wanted public List<String> participants
to be public List<Person> participants
but that can wait till the relation and deserialization works.
Solution
So, I found out what I did do wrong. Two things:
- Use a valid json:
curl --location --request POST 'localhost:8080/trace' \
--header 'Content-Type: application/json' \
--data-raw '{
"traceOwner": {"name": "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"
}'
Please notice the different "traceOwner" part.
- Use
@ManyToOne(cascade = CascadeType.ALL)
to also update the transient property when persisting.
Answered By - maiksensi