Reputation: 2421
I am trying to retrieve OneToMany association in my spring boot project.When I am returning the JSON response from controller it is only getting like normal list of string instead of proper JSON. This result is a join result from a JPQL query,
I am adding the repository method here,
@Query("SELECT ur.userId , r.role FROM Roles r JOIN r.roleJoin ur")
List<Roles> findByRole();
And my controller has the code like the following,
@GetMapping("/check")
public List<Roles> check() {
return repoObj.findByRole();
}
And getting response like this,
[[2,"A"],[649,"B"],[651,"C"],[653,"A"],[658,"A"],[3,"A"],[1,"B"],[670,"B"]]
It is seemd to be a list of object, But defaultly spring boot controller will return the data in JSON format. But I only getting like the following. Since I need to access the JSON from my front end Angular application.
Can anyone help me to clarify that to send response in proper JSON itself instead of just a list ?
Upvotes: 1
Views: 893
Reputation: 36123
The problem is that the projection in your query does not match the return type.
You assume that you get objects of type Roles but in your query you only select ur.nuserId and r.srole_desc
Either change your query to:
@Query("SELECT r FROM Roles r JOIN r.roleUserMappingJoin ur")
but this would be the same as findAll()
or create a DTO that holds the data you want to return.
Read more about projection and Spring Data JPA here:
https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#projections
Upvotes: 1