spring boot does not serialize json

3

I am developing a web application with spring boot. When I make a query using a jparepositoy of the findAll type, I intend to obtain the list of users and their corresponding role. In the response obtained, all the properties of my entity Users are correctly converted to json , except for the property that encapsulates the role object.

These are the Users and Roles entities:

@Entity
@Table(name="usuarios")
public class Usuarios { 
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    int id_usuario;

    @Column(name ="name")
    String name;

    @Column(name ="surname")
    String surname;

    @Column(name ="login")
    String login;

    @Column(name ="password")
    String password;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name ="rol")
    @JsonIgnore
    //@JsonBackReference
    private Roles rol;


    public Usuarios() {

    }
}
package es.pdv.daw.proyect.beans;
@Entity
@Table(name="roles")
public class Roles {    
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int idRol;

    @Column(name ="rol_name")
    private String rolName;

    @OneToMany(mappedBy ="rol", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    //@JsonManagedReference
    private List<Usuarios> usuarios= new ArrayList<>();

    public Roles() {

    }
}
package es.pdv.daw.proyect.controller;

@RestController
public class ConsultasRestController {

    /**
     * Propiedad que encapsula el objeto de acceso a DAO.
     */
    @Autowired
    private LoginService loginService;

    @RequestMapping(value="dameUsuarios",method = RequestMethod.GET)
    public List<Usuarios> loadArticulos(List<Usuarios> usuarios){


    return loginService.findAllUsers(); 
    }   

}

This is the json obtained. As you can see, the role has not been included.

[{"name":"Jose Antonio","surname":"Bernabe Duran","login":"bubu","password":"bubu","idUsuario":1},{"name":"Alexis","surname":"Bernabe Pineda","login":"alex","password":"2010","idUsuario":2}]
    
asked by Jose Antonio 19.07.2016 в 12:24
source

3 answers

1

Finally I found the error. I had forgotten to implement the serializable interface to entities and after modifying this remove the tags @JsonIgnore , @JsonManagedReference and @JsonBackReference and everything works correctly.

Users

@Entity
@Table(name="usuarios")
public class Usuarios implements Serializable {


    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    int id_usuario;

    @Column(name ="name")
    String name;

    @Column(name ="surname")
    String surname;

    @Column(name ="login")
    String login;

    @Column(name ="password")
    String password;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name ="rol")
    private Roles rol;


    public Usuarios() {

    }

Roles

@Entity
@Table(name="roles")
public class Roles implements Serializable  {


    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private int idRol;

    @Column(name ="rol_name")
    private String rolName;

    @OneToMany(mappedBy ="rol", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private List<Usuarios> usuarios= new ArrayList<>();

    public Roles() {

    }

JSON

[  
   {  
      "name":"Jose Antonio",
      "surname":"Bernabe Duran",
      "login":"bubu",
      "password":"bubu",
      "rol":{  
         "idRol":1,
         "rolName":"Administrador"
      },
      "idUsuario":1
   },
   {  
      "name":"Alexis",
      "surname":"Bernabe Pineda",
      "login":"alex",
      "password":"2010",
      "rol":{  
         "idRol":2,
         "rolName":"Usuario"
      },
      "idUsuario":2
   },
   {  
      "name":"Maria Luisa",
      "surname":"Pineda Uber",
      "login":"ml",
      "password":"1979",
      "rol":{  
         "idRol":1,
         "rolName":"Administrador"
      },
      "idUsuario":3
   },
   {  
      "name":"Rafael",
      "surname":"Martin Lopez",
      "login":"Rafa",
      "password":"rafa",
      "rol":{  
         "idRol":2,
         "rolName":"Usuario"
      },
      "idUsuario":4
   }
]
    
answered by 20.07.2016 в 18:26
0

It's because the ROL property you have annotated it with @JsonIgnore which means that this field is not serialized.

    
answered by 19.07.2016 в 12:39
0

Try changing the Role reference in User to:

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name ="rol")
//@JsonIgnore quitamos ignore   
private Roles rol;

And we add the ignore to:

@OneToMany(mappedBy ="rol", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JsonIgnore 
private List<Usuarios> usuarios= new ArrayList<>();
    
answered by 19.07.2016 в 15:41