how to de serialize a setter less property using Jackson in Java

Viewed 10778

I am using Spring boot web service, @requestbody.While converting JSON to Java Object throwing error message as "Problem deserializing 'setterless' property"

my POJO is like this.

Class test 
{
  private List<AdminRequest> _AdminRequestSet;
  public void addAdminRequest(AdminRequest param)
   {
      if (_AdminRequestSet == null)
      {
         _AdminRequestSet = new Vector<AdminRequest>();
      }
      this._AdminRequestSet.add(param);
   }
   public void removeAdminRequest(AdminRequest param)
   {
      this._AdminRequestSet.remove(param);
   }
   public List<AdminRequest> getAdminRequestSet()
   {
      return this._AdminRequestSet;
   }
   public void clearAdminRequest()
   {
      if (_AdminRequestSet == null)
      {
         return;
      }
      _AdminRequestSet.clear();
   }   
}
2 Answers

Option 4: Remove unnecessary getter/setter.

Consider this situation:

@JsonProperty("data")
private List<AdminRequest> _AdminRequestSet;

public List<AdminRequest> getAdminRequestSet()
{
  return _AdminRequestSet;
}

The problem is that, Jackson tried to match the getter/setter pattern on the member, adminRequestSet, but it can't find the data member. Hence the exception.

Two things you need to consider:

  1. Do you actually need to getAdminRequestSet() method? If so, annotate it with @JsonIgnore. Let the @JsonProperty("data") to generate the list as "data".

  2. Search usage to determine whether you need the getXXX() method. Remove it if you don't need it. You don't even have to annotate with @JsonIgnore.

Related