Skip to main content
  1. About
  2. For Teams
Asked
Viewed 1k times
1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I can try... But do you think I really need this? Because, I already have json format when my query is not custom. For example with findAll(); [{"id":1,"name":"Tom Wally"}]
I understand you need a specific json response [{"name":"Tom Wally"}], so, as you return a List<User> then you need to build a response as an arraylist with only specific fields you want to return in your json response.
there is some ways to return specific fields between objects, for example, you do not need id field, so you need a class without id field. I suggest you to see orika , it is a mapper tool to send attributes between objects. http://orika-mapper.github.io/orika-docs/index.html

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.