@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?