4

In Spring 3.3 I have an entity which is mapped to a database table. In this entity class I have all properies annotated with @JsonProperty, for instance @JsonProperty("ID"). Stepping into the controller a service is called to get such an entity by using a DAO/repository. This works well but when I send this entity back to the requestor using @ResponseBody all properties are sent twice. Once as demanded but one more time beginning lowercase until the first camel case letter occurs. An example...

public class MyEntity {
    @JsonProperty("MYSpecialSuperId")
    private String MYSpecialSuperId;

    ...

    public String getMYSpecialSsuperId() {
        return this.MYSpecialSuperId;
    }

}

After JSON stringifying the result is:

{ "MYSpecialSuperId":""9", "myspecialSuperId":"9" }

Why is the property twice in the result and why is the lettering different???

BTW: It was not my idea to let Java properties begin with an uppercase letter even yet with more than one uppercase letter.

du-it
  • 2,561
  • 8
  • 42
  • 80
  • I think this is the same issue: http://stackoverflow.com/questions/7557397/jackson-json-java-class-fields-are-serialized-multiple-times, or even this: http://stackoverflow.com/questions/7105745/how-to-specify-jackson-to-only-use-fields-preferably-globally – Katona Sep 25 '13 at 20:39

2 Answers2

9

Jackson's ObjectMapper uses the Java bean pattern. In other words, it expects the following

public class Foo {
    public Object bar;

    public Object getBar() {...}

    public void setBar(Object bar) {...}
}

The getters and setters start with get and set, respectively, followed by the corresponding field name with its first letter capitalized. If you change your code to

public class MyEntity {
    @JsonProperty("MYSpecialSuperId")
    private String mySpecialSuperId;

    ...

    public String getMySpecialSuperId() {
        return this.mySpecialSuperId;
    }

}

Note that the field starts with lowercase my instead of uppercase (regardless of the @JsonProperty value), I removed the extra s in getMYSpecialSsuperId and used a lowercase y. So now the field name matches the getter name and jackson knows that the property is the same and doesn't need to serialize twice.

If you have no choice, you can follow what Katona posted in the comments and use

@JsonAutoDetect(getterVisibility=Visibility.NONE)

to make jackson ignore the getters completely and only use the fields to serialize your JSON.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • Well, I don't have the option to change the property names (but I'm working on this ;) ). Thus, some properties are named like this: ABCDOneOfMyProperty (I hate it but I have to live with it). The @JsonAutoDetect(getterVisibility=Visibility.NONE) annotation did the trick. Thank you. :)) – du-it Sep 26 '13 at 12:38
1

I think you have a typo in your accessor; if it has "SsuperId" it does not match name of the field; and as such field and getter are taken to mean different logical properties.

StaxMan
  • 113,358
  • 34
  • 211
  • 239
  • This alone does not fix the overall problem. – Sotirios Delimanolis Sep 26 '13 at 02:04
  • The problem is due to mismatch between name extracted from getter, field name: I am guessing it is due to non-standard naming (multiple capital letters as prefix). You may need to repeat `@JsonProperty` annotation for getter. – StaxMan Sep 26 '13 at 03:11
  • I'm sorry. My fingers are to blame for this typo. ;) In real code I don't have this typo ... only here when asking this question. – du-it Sep 26 '13 at 12:09
  • I understand that. So that typo was not the cause. But what I am saying is that regardless, names are mismatching wrt case differences between field name, and "mangled" getter name, because of multiple capital letters starting the name -- unfortunate (and perhaps even flaw in Jackson's name mangling, wrt Bean specification?), but that's what it is. – StaxMan Sep 27 '13 at 17:26