Error deserialize JSON - How to Resolve?

Asked

Viewed 826 times

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 post to persist an object in the database, I get this exception: error postman

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.

  • Can you put the error text? It looks better to read, maybe even to copy the text for a reply

  • @Jeffersonquesado javax.ws.rs.Processingexception: Error deserializing Object from Entity stream. javax.json.bind.Jsonbexception: Error deserialize JSON value into type: class com.emerich.model.Category.

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 is why my entities have jpa’s Manytoone and Onetomany relationship and my api was not treating the deserialiazation of these objects. Taking advantage of the solution, there is some way to automatically treat this in the api?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.