当前位置: 首页 > 知识库问答 >
问题:

错误:找不到类org的序列化程序。json。JSONObject,未发现创建BeanSerializer的属性

颛孙天宇
2023-03-14

我得到这个错误“嵌套异常是org.springframework.http.converter.HttpMessageConversionException:类型定义错误:[简单类型,类org.json.JSONObject];嵌套异常是com.fasterxml.jackson.databind.exc.InvalidDefitionException:运行项目时,没有为类org.json.JSONObject找到序列化程序,也没有发现创建BeanSerializer的属性”。我尝试了通过这个平台找到的许多解决方案,但这些都不适合我。是这样还是其他情况?如果是这样,请帮助我解决这个问题。

下面是RowEvent。java类。

@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;
    }
}

支持Response.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;
        }
}

共有1个答案

李光华
2023-03-14

似乎您正在使用json.org依赖项来序列化JSON,但是Spring已经提供了另一个json库(jackson-datind),并且jackson不知道如何将JSONObject从org.json转换为Json String。

要解决此问题,您可以使用ObjectNode,即Jackson Database ind中的JSONObject等价物:

@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;
    }
}
 类似资料: