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}]