Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

I'm using jackson to convert an object of mine to json. The object has 2 fields:

@Entity

public class City {

   @id

   Long id;

   String name;

   public String getName() { return name; }

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

   public Long getId() { return id; }

   public void setName(Long id){ this.id = id; }

}

Since I want to use this with the jQuery auto complete feature I want 'id' to appear as 'value' in the json and 'name' to appear as 'label'. The documentation of jackson is not clear on this and I've tried every annotation that even remotely seems like it does what I need but I can't get name to appear as label and id to appear as value in the json.

Does anyone know how to do this or if this is possible?

1 Answer

0 votes
by (46k points)

Have you tried using @JsonProperty?

@Entity

public class City {

   @id

   Long id;

   String name;

   @JsonProperty("label")

   public String getName() { return name; }

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

   @JsonProperty("value")

   public Long getId() { return id; }

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

}

Related questions

0 votes
2 answers
0 votes
1 answer
0 votes
1 answer
asked Nov 12, 2019 in Java by Anvi (10.2k points)
0 votes
1 answer
asked Oct 27, 2019 in Java by Shubham (3.9k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...