Java-POJO2.0
我整天都在扫描堆栈溢出,还没有找到解决问题的可行方案 我有一个具有基本类型和嵌套对象的pojo。例如Java-POJO2.0,java,json,serialization,jackson,deserialization,Java,Json,Serialization,Jackson,Deserialization,我整天都在扫描堆栈溢出,还没有找到解决问题的可行方案 我有一个具有基本类型和嵌套对象的pojo。例如 @JsonIgnoreProperties({"duration", "errorCode", "haveFieldsChanged", "serviceRequestToken", "storedProcDuration"}) // Abstract Base Class properties class Bus extends AbstractBaseClass implements
@JsonIgnoreProperties({"duration", "errorCode", "haveFieldsChanged",
"serviceRequestToken", "storedProcDuration"}) // Abstract Base Class
properties
class Bus extends AbstractBaseClass implements Serializable{
@JsonIgnore
private static final long serialVersionId = 1;
@JsonProperty("name")
String name;
@JsonProperty("id")
int id;
@JsonProperty("students")
List<Student> students; // Nested Objects
@JsonProperty("employer")
Employer employer; //Nested object
// Getters and setters - none are annotated
@JsonRootName(value = "student")
class Student implements Serializable{
// student fields
}
@JsonRootName(value = "statusType")
class Employer implements Serializable{
@JsonProperty("id")
int id;
}
我尝试过@JsonProperty、@JsonSerialize(as=Employer.class),我尝试过为Employer对象构建映射。我觉得我已经用尽了大部分的选择。我有什么遗漏吗
我在尝试其他注释时遇到堆栈溢出异常。我很感激能得到的任何帮助
我将@JsonProperty添加到字段的原因是为了帮助驱动反序列化。我认为这可能是序列化的根本原因,但我不确定
序列化实现
private String serializeBus(Bus bus) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
String json = null;
try {
json = mapper.writeValueAsString(bus);
} catch (JsonProcessingException e) {
logger.error("Error serializing bus");
throw new Exception(e);
}
return json;
}
谢谢大家! 因此,如果您无法共享代码,那么我将您的示例放在上面的一个小沙箱项目中: 它目前使用SpringBoot2.1运行,但将托管Jackson版本降级到2.6.1也没有负面影响
也许您可以尝试使用一个简单的
mvn clean verify
或手动测试运行de.mle.stackoverflow.jackson.BusSerializerTest
来深入研究这个问题。因此,如果您无法共享代码,那么我将您的示例放在一个小沙盒项目中:
它目前使用SpringBoot2.1运行,但将托管Jackson版本降级到2.6.1也没有负面影响
也许你可以尝试使用一个简单的
mvn clean verify
或手动测试运行de.mle.stackoverflow.jackson.BusSerializerTest
来深入研究这个问题。。您是否使用任何注释或其他类型的配置?您使用哪个版本的库?您问题中的所有代码都很好,这不是员工未序列化的原因。你能给我们看一个Github repo或者类似的东西,比如你如何序列化bus对象吗?employer
可以为空吗?我在所有类中都有getter和setter。我使用了一些注释。请参考我上面的示例代码@幸运的是,该代码是专有的,因此无法将其推送到github@mleI已经用getter和setter创建了类似的结构,它也为我的雇主打印。您是否使用任何注释或其他类型的配置?您使用哪个版本的库?您问题中的所有代码都很好,这不是员工未序列化的原因。你能给我们看一个Github repo或者类似的东西,比如你如何序列化bus对象吗?employer
可以为空吗?我在所有类中都有getter和setter。我使用了一些注释。请参考我上面的示例代码@幸运的是,该代码是专有的,因此无法将其推送到github@最大似然误差
private String serializeBus(Bus bus) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE);
mapper.setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
String json = null;
try {
json = mapper.writeValueAsString(bus);
} catch (JsonProcessingException e) {
logger.error("Error serializing bus");
throw new Exception(e);
}
return json;
}