4

I am working with a simple Spring Boot based RestController. I am returning JSON, but I am unable to control the name of the keys generated in the response. The POJO looks like this:

public class SomePojo {
   @JsonProperty("name")
   private String fullName;
   @JsonProperty("name")
   public String getFullName() {
     return fullName;
   }
   public void setFullName(String fullName) {
     this.fullName = fullName;
   }
}

If I create a new instance as follows:

SomePojo sm = new SomePojo();
sm.setFullName("John Doe");

and return the instance in the @ResponseBody. I expect to see

{ "name" : "John Doe" }

but I am seeing

{ "fullName" : "John Doe" }

I tried using the @JsonProperty("name") annotation on both the property as well as its getter, but it is not working. Spring Boot version is 1.4.2. Any suggestions as to what I am missing?

Web User
  • 6,996
  • 13
  • 57
  • 86

2 Answers2

6

Set the @JsonProperty in getter and setter it will work

VelNaga
  • 3,277
  • 5
  • 42
  • 74
0

What if you change this.fullName to this.name

public void setFullName(String fullName) { this.fullName = fullName; }

nish
  • 948
  • 4
  • 15
  • 33