Home > database >  Error: No serializer found for class org.json.JSONObject and no properties discovered to create Bean
Error: No serializer found for class org.json.JSONObject and no properties discovered to create Bean

Time:03-08

I am getting this error "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" when running the project. I tried many solutions I found through this platform but those are didnt worked for me. Is it this is the case or any other? If so please help me to solve this issue.

Below is the RowEvent.java class.

@Component("support.support-provider.row-event")
public class RowEventSupportProvider extends SupportProvider {

    @Override
    public SupportResponse search(SearchData searchData) throws JSONException {
        JSONObject supportProvider = new JSONObject();
        supportProvider.put("startDate", searchData.getStartDate());
        supportProvider.put("startTime", searchData.getStartTime());
        supportProvider.put("endDate", searchData.getEndDate());
        supportProvider.put("endTime", searchData.getEndTime());
        supportProvider.put("clientId", searchData.getClientId());
        supportProvider.put("launchContextId", searchData.getLaunchContextId());
        supportProvider.put("integrationId", searchData.getIntegrationId());
        supportProvider.put("deploymentId", searchData.getDeploymentId());
        supportProvider.put("courseId", searchData.getCourseId());
        supportProvider.put("courseType", searchData.getCourseType());
        supportProvider.put("assignmentId", searchData.getAssignmentId());
        supportProvider.put("assignmentType", searchData.getAssignmentType());
        supportProvider.put("userId", searchData.getUserId());
        supportProvider.put("userType", searchData.getUserType());

        SupportResponse supportResponse = new SupportResponse();
        supportResponse.setData(supportProvider);
        return supportResponse;
    }
}

SupportResponse.java

    import com.fasterxml.jackson.annotation.JsonAutoDetect;
import org.json.JSONObject;
import org.springframework.hateoas.Links;
import org.springframework.hateoas.RepresentationModel;

import java.util.List;
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class SupportResponse extends RepresentationModel<SupportResponse>
{
        String id;
        Object data;
        List<Links> links;

        public String getId() {
                return id;
        }

        public void setId(String id) {
                this.id = id;
        }

        public Object getData() {
                return data;
        }

        public void setData(Object data) {
                this.data = data;
        }
}

CodePudding user response:

It seems that you are using the json.org dependency to serialize JSON, but spring already come with another json library (jackson-databind) and jackson don't know how to translate a JSONObject from org.json into a Json String.

To resolve this issue, you can use ObjectNode, the JSONObject equivalent in Jackson Databind:

@Component("support.support-provider.row-event")
public class RowEventSupportProvider extends SupportProvider {
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public SupportResponse search(SearchData searchData) throws JSONException {
        ObjectNode supportProvider = objectMapper.createObjectNode();
        supportProvider.put("startDate", searchData.getStartDate());
        supportProvider.put("startTime", searchData.getStartTime());
        supportProvider.put("endDate", searchData.getEndDate());
        supportProvider.put("endTime", searchData.getEndTime());
        supportProvider.put("clientId", searchData.getClientId());
        supportProvider.put("launchContextId", searchData.getLaunchContextId());
        supportProvider.put("integrationId", searchData.getIntegrationId());
        supportProvider.put("deploymentId", searchData.getDeploymentId());
        supportProvider.put("courseId", searchData.getCourseId());
        supportProvider.put("courseType", searchData.getCourseType());
        supportProvider.put("assignmentId", searchData.getAssignmentId());
        supportProvider.put("assignmentType", searchData.getAssignmentType());
        supportProvider.put("userId", searchData.getUserId());
        supportProvider.put("userType", searchData.getUserType());

        SupportResponse supportResponse = new SupportResponse();
        supportResponse.setData(supportProvider);
        return supportResponse;
    }
}
  • Related