我得到这个错误“嵌套异常是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;
}
}
似乎您正在使用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;
}
}
有来自web服务的JSON,JSON数组作为响应 在JsonArray中获取响应后,读取Json数组的Json对象时出错: 没有找到JSONObject类org.json.序列化程序,也没有发现创建BeanSerializer的属性(为了避免异常,禁用SerializationConfig. Feature.FAIL_ON_EMPTY_BEANS))
这个POJO对象以json对象的形式提供给前端。但是,在前端提取数据时,出现了此错误。 当我在getJSONObject中添加@JsonIgnore时,错误就消失了。getJSONObject方法被认为是getter方法,杰克逊也试图序列化它。我想了解杰克逊的这种行为,以及为什么@JsonIgnore正在纠正错误?
我有一个类的数组列表,如下所示: 我想把这个列表写成Json,并尝试了以下代码: 并收到以下错误信息: 找不到类~~~~~的序列化程序,也找不到创建BeanSerializer的属性(为了避免异常,请禁用SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:java.util.ArrayList[0])` 我尝试添加但由于未知原因,它使所有Person对象
问题内容: 从Web服务获取Json Array的JSON作为响应 在JsonArray中获取响应后,在读取Json Array的Json对象时出现错误: 找不到针对类org.json.JSONObject的序列化程序,也没有发现创建BeanSerializer的属性(为避免异常,请禁用SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS)) 问题答案:
无法编写JSON:找不到org.json.jsonObject类的序列化程序,也找不到创建BeanSerializer的属性(为了避免异常,禁用SerializationFeature.fail_on_empty_beans) UserService.getUserList():
问题内容: 从Web服务获取Json Array的JSON作为响应 在JsonArray中获得响应后,在读取Json Array的Json对象时出现错误: 找不到针对类org.json.JSONObject的序列化程序,也没有发现创建BeanSerializer的属性(为避免异常,请禁用SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS)) 问题答案: