Duplicate fields in json due to inconsistent capitalization of the first 2 letters in the field name

400 views
Skip to first unread message

ajay mundru

unread,
Aug 4, 2020, 2:24:58 PM8/4/20
to jackson-user
In Event class POJO

@JsonProperty("aDocId")
private String aDocId = null;
  
public String getADocId() {
    return aDocId;
  }

  public void setADocId(String aDocId) {
    this.aDocId = aDocId;
  }

-------------------------------------------------------------------------------------
While doing a post request,

Event event = (Event) object;
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Event> entity = new HttpEntity<>(event, headers);
String response = restTemplate.postForObject(url, entity, String.class);
--------------------------------------------------------------------------------------
response says message cannot be accepted as it has unidentified field 

As the json getting posted has duplicate fields with different case: aDocId, adocId where as I expected only aDocId as per my Event class.

I could not change anything in the POJO as it is a auto generated class based on swagger.

I am using Spring boot. Seeking for help to resolve this scenario.

Tatu Saloranta

unread,
Aug 4, 2020, 2:37:32 PM8/4/20
to jackson-user
The problem here is that capitalization of `aDocId` is problematic and
does not really match implicit name that getter/setter would produce.
Because of this, Jackson things there are 2 different properties (with
different casing).
This only affects the specific case of single-lower-case-letter.

There are 2 ways to resolve this:

1. Move `@JsonProperty` annotation to getter or setter -- there is no
need to annotate Field, usually, and since it is private, it should be
ignore if not annotated
2. Add duplicate of `@JsonProperty` on getter or setter -- this will
link Field and getter/setter Methods to make them be part of one
single logical propery

I hope this helps,

-+ Tatu +-

ajay mundru

unread,
Aug 4, 2020, 3:11:47 PM8/4/20
to jackson-user
But Event class i have is auto generated and I cannot add annotation on the getter method. Is there any other solution to make this work?

Tatu Saloranta

unread,
Aug 4, 2020, 4:40:40 PM8/4/20
to jackson-user
On Tue, Aug 4, 2020 at 12:11 PM ajay mundru <ajju...@gmail.com> wrote:
>
> But Event class i have is auto generated and I cannot add annotation on the getter method. Is there any other solution to make this work?

You could use mix-in annotations (see f.ex
https://www.baeldung.com/jackson-annotations)
that can be attached without modifying actual classes.

-+ Tatu +-
> --
> You received this message because you are subscribed to the Google Groups "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/bfcbb3f3-4956-4f8d-88c8-5fb2032feaf9o%40googlegroups.com.

ajay mundru

unread,
Aug 12, 2020, 6:49:18 PM8/12/20
to jackson-user
Thanks will try this.
Reply all
Reply to author
Forward
0 new messages