Entity properties serialized twice
Asked Answered
L

3

5

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.

Lookthrough answered 25/9, 2013 at 20:7 Comment(1)
I think this is the same issue: #7557897, or even this: #7106245Ilan
M
10

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.

Mcnelly answered 26/9, 2013 at 2:9 Comment(1)
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. :))Lookthrough
H
0

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.

Harrumph answered 26/9, 2013 at 1:41 Comment(4)
This alone does not fix the overall problem.Mcnelly
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.Harrumph
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.Lookthrough
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.Harrumph
L
0

I have resolved this by adding the same @JsonProperty annotation to getter and setter.

So in your example doing this should resolve your issue and you will only have it once in the JSON object.

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

    ...

    @JsonProperty("MYSpecialSuperId") // <-- This is added
    public String getMYSpecialSsuperId() {
        return this.MYSpecialSuperId;
    }

}
Ladylove answered 2/8, 2024 at 10:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.