4

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();
   }   
}
Danda
  • 380
  • 1
  • 2
  • 14

2 Answers2

3

The class test is not Java bean conform. The name of the field _AdminRequestSet does not correspond to the getter getAdminRequestSet. Jackson supports the serialization without setters, but it must be possible to map from the property name in the json to a field in the target class.

For example we have this json:

{"adminRequestSet": [{"id":1},{"id":2}]}

Jackson is able to find the getter getAdminRequestSet in class test, which corresponds to the property adminRequestSet in the json. But there is no setter setAdminRequestSet and no field adminRequestSet in the class test.

Now you have different options. Here a list of common solutions:

Option 1: rename the field _AdminRequestSet to adminRequestSet (so that class test is java bean conform)

Option 2: use an annotation to specify the property name for a field

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

Option 3: If you are not able to change the class test, you could use Jackson MixIns

abstract class testMixIn {
    @JsonProperty("adminRequestSet") 
    private List<AdminRequest> _AdminRequestSet;
}

//register testMixin for class test
objMapper.addMixIn(test.class, testMixIn.class);
Community
  • 1
  • 1
Meiko Rachimow
  • 4,664
  • 2
  • 25
  • 43
0

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.

simonso
  • 595
  • 1
  • 8
  • 19