0

When I get List from server with spring I get in client object user like this:

{
  "id": 1,
  "name": "hgfhj",
  "age": 120,
  "createdDate": 1457211138000,
  "admin": true
}

UserController.java method:

@RequestMapping(value = "/user/", method = RequestMethod.GET)
    public ResponseEntity<List<User>> getList() {

        List usersList = userService.getList();

        ResponseEntity<List<User>> respEntity = null;

        if(usersList.isEmpty()){
            respEntity =new ResponseEntity<List<User>>(HttpStatus.NO_CONTENT);
            return respEntity;
        }

        respEntity =new ResponseEntity<List<User>>(usersList, HttpStatus.OK);

        return respEntity;

    }

And when I use Gson I get in client object user like this:

{
  "id": 1,
  "name": "hgfhj",
  "age": 120,
  "isAdmin": true,
  "createdDate": "Mar 5, 2016 10:52:18 PM"
}

UserController.java method:

 @RequestMapping(value = "/user/", method = RequestMethod.GET)
    public String getList() {

        List usersList = userService.getList();

        ResponseEntity<List<User>> respEntity = null;

          respEntity =new ResponseEntity<List<User>>(usersList, HttpStatus.OK);

        Gson gson = new Gson();
        String json = gson.toJson(usersList);

        return json;
    }

In all project user property name "isAdmin", I do not understand why it's changed to "admin". How can I use spring but get in client "isAdmin" without gson?

User.java:

@Entity
public class User {
    /*@Column(name="id")*/
    @Id
    @GeneratedValue
    private int id;

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

    @Column(name="age")
    private int age;

    @Column(name="isAdmin")
    private boolean isAdmin;

    @Column(name="createdDate")
    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "dd.MM.yyyy")
    private Date createdDate;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }

    public boolean isAdmin() {
        return isAdmin;
    }

    public Date getCreatedDate() {
        return createdDate;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void setIsAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
    }

    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }
}
5
  • Can you please explore User class little bit ? So that we get more idea to fix this. Commented Mar 23, 2016 at 19:28
  • @vishal gajera, I've added User class Commented Mar 23, 2016 at 19:30
  • what you want into response ? isAdmin or admin ? Commented Mar 23, 2016 at 19:40
  • @vishal gajera, isAdmin Commented Mar 23, 2016 at 19:47
  • please see my answer. Commented Mar 23, 2016 at 19:55

2 Answers 2

1

please do following changes into your User class's setter method,

@JsonProperty("isAdmin") // i guess you want isAdmin into response..
public void setIsAdmin(boolean isAdmin) {
        this.isAdmin = isAdmin;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Annotate your User objects attributes with @JsonProperty to spesify the name you want as output.

Example

public class User {
    ...

    @SerializedName("isAdmin")
    @Column(name="isAdmin")
    private boolean admin;

    ...
}

this will return something like

{
    "isAdmin" : true
}

for more information: http://www.javacreed.com/gson-annotations-example/


Updated: For future reference @JsonProperty("name") needs to be on the getters with gson, not the attributes.

6 Comments

Now i get: ` { "id": 1, "name": "hgfhj", "age": 120, "createdDate": 1457211138000, "admin": true, "isAdmin": true }`
Dont add the attribute.. You need to add the line to your User class before @Column(name="isAdmin")
I haven't added new attribute, just added @JsonProperty("isAdmin") as you said
this is dependant on what parser you are using. try to use @SerializedName("isAdmin") instead of @JsonProperty(..)
This returns: { ` "admin" : true` }
|

Your Answer

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.