Skip to content

Instantly share code, notes, and snippets.

@ataube
Last active August 29, 2015 14:16
Show Gist options
  • Save ataube/bedee4779bad072a3fa9 to your computer and use it in GitHub Desktop.
Save ataube/bedee4779bad072a3fa9 to your computer and use it in GitHub Desktop.
Spring Date Rest - Custom Deserialization with entity associations problem
@Entity
public class EntityA {
private String name;
}
@Entity
public class EntityB {
@OneToOne
private EntityA entityA;
}
public class EntityBDeserializer extends JsonDeserializer<EntityB> {
@Override
public EntityB deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
SpecialEntityBJson obj = mapper.readValue(jp, SpecialEntityBJson.class);
//Problem: obj.entityA will not be resolved, because the type SpecialEntityBJson is not a PersistentEntity (see AssociationUriResolvingDeserializerModifier)
return new EntityB(obj.entityA, //etc...);
}
//Some special Json format which does not fit to the domain model
private static class SpecialEntityBJson {
public EntityA entityA;
}
}
//Tests
//curl -X POST -H "Content-Type: application/json" -d '{"name": "foobar"}' http://localhost:8080/entityA
//curl -X POST -H "Content-Type: application/json" -d '{"entityA": "http://localhost:8080/entityA/1"}' http://localhost:8080/entityB
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment