Tom Wally
Tom Wally

Reputation: 552

How to add column name to the query result SPRING JPA

For example, I've got such query:

@Query("SELECT u.name FROM User u")
public List<User> findAllUsers();

I know, this is not an optimal way to show all users, but it's just for example, my query is more complex.

I've got such answer: [{"Tom Wally"}]

But I want to get: [{"name":"Tom Wally"}]

How to add column name to custom query?

Upvotes: 1

Views: 1002

Answers (1)

duardito
duardito

Reputation: 107

Maybe you are talking about how return a response in a json format.

If you want a response in json-format you should create two classes like these, one when creating your object, and the other when creating the response from a list.

public class UserResponseList extends ArrayList<UserResponse>(){

public UserResponseList(List <UserResponse> myList){ 
super(myList);
}

}
public class UserResponse(){

private String name;
}

finally ,you instantiate UserResponseList sending your list in constructor, and you have your json response with your specific format.

Upvotes: 1

Related Questions