Sarthak Mittal
Sarthak Mittal

Reputation: 155

Getting this error No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer

@Entity
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class SomeRandomEntity {
  private String var1;
  private String var2;
  private String var3;
  public JSONObject getJSONObject throws JSONException {
        JSONObject properties = new JSONObject();
        properties.put("var1", getVar1());
        properties.put("var2", getVar2());
        properties.put("var3", getVar3());
        return properties;
    }
}

This POJO object is being given to frontend in the form of json object. But while fetching the data in the frontend this error is appearing.

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class org.json.JSONObject]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[0]->com.artifact.group.SomRandomDTO["jsonobject"])] with root cause 
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.json.JSONObject and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.data.domain.PageImpl["content"]->java.util.Collections$UnmodifiableRandomAccessList[0]->com.artifact.group.SomRandomDTO["jsonobject"])

The error goes away when I add @JsonIgnore in the getJSONObject. The getJSONObject method is being considered a getter method and jackson tries to serialize that too. I want to understand this behaviour of jackson and why @JsonIgnore is rectifying the error?

Upvotes: 2

Views: 11830

Answers (1)

hiren
hiren

Reputation: 1812

Here your object when you return it in response is getting Serialized to json string using ObjectMapper which is used in spring's MessageConverter(i.e Jackson2HttpMessageConverter) bean. Now the error is caused due to how ObjectMapper serializes your class. Your class has 4 filed, 3 of type String and 1 of type JSONObject. ObjectMapper when serializing fields, tries to find the corresponding serializer based on the field type. There are some out-of-the-box implementation of serializer for known type like String but for your custom type you either need to provide serializer to ObjectMapper bean via configuration of set property SerializationFeature.FAIL_ON_EMPTY_BEANS to false.

ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);

To verify this you can change the return type of method getJSONObject to String (as shown below) and your code will work.

public String getJSONObject throws JSONException {
        JSONObject properties = new JSONObject();
        properties.put("var1", getVar1());
        properties.put("var2", getVar2());
        properties.put("var3", getVar3());
        return properties.toString();
    }

Upvotes: 2

Related Questions