Error deserialize JSON - How to solve?

0

I'm using jpa and I have these two entities:

@Entity
@JsonIdentityInfo(
  generator = ObjectIdGenerators.PropertyGenerator.class, 
  property = "id")

public class Categoria implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @NotNull
    private int id;
    private String tipo;

    @JsonBackReference
    @OneToMany(mappedBy = "categoria_id")
    private List<Post> categoria_post;
}


    @Entity
@JsonIdentityInfo(
  generator = ObjectIdGenerators.PropertyGenerator.class, 
  property = "id")
public class Post implements Serializable {
    @Id
    @GeneratedValue(strategy =GenerationType.IDENTITY)
    private int id;
    @NotNull
    private String titulo;
    @NotNull
    @JsonManagedReference
    @ManyToOne
    private Categoria categoria_id;
    @NotNull
    private String descricao;
    private String img;
    @ManyToOne
    private Usuario post_usuario;
    private int likes;
}

When I make a post to persist an object in the database, I get this exception:

javax.servlet.ServletException: javax.ws.rs.ProcessingException: Error deserializing object from entity stream. root cause

javax.ws.rs.ProcessingException: Error deserializing object from entity stream. root cause

javax.json.bind.JsonbException: Error deserialize JSON value into type: class com.emerich.model.Category.

    
asked by anonymous 28.04.2018 / 22:45

1 answer

1

Good morning. The problem was solved, the structure of my JSON was incorrect:

Correct:

{
        "titulo": "Combatendo a Fome",
        "categoria_id": {"id":"1"},
        "descricao": "Projeto Destinado a distribuição cestas básicas..",
        "img": "fome.png",
        "post_usuario": {"id":"1"},
        "likes": 12

    }

Incorrect:

{
    "titulo": "Combatendo a Fome",
    "categoria_id": 1,
    "descricao": "Projeto Destinado a distribuição cestas básicas..",
    "img": "fome.png",
    "post_usuario": 1,
    "likes": 12

}

That's because my entities have ManyToOne and OneToMany relationship of the jpa and my api was not dealing with deserialiazation of these objects. Taking advantage of the solution, is there any way to handle this automatically in api?

    
29.04.2018 / 14:31