gpt4 book ai didi

java - 使用 Jackson 生成 JSON 模式进入无限循环

转载 作者:行者123 更新时间:2023-11-29 09:11:37 27 4
gpt4 key购买 nike

可能是我在 Jackson 中遗漏了一个明显的注释,但我有以下类(class)

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.jsonschema.JsonSchema;

@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Loop {

private String name;
private Loop otherLoop;

public String getName() { return name; }
public void setName(String name) { this.name = name; }
public Loop getOtherLoop() { return otherLoop; }
public void setOtherLoop(Loop otherLoop) { this.otherLoop = otherLoop; }

public static void main(String[] args) throws Exception {
Loop parent = new Loop();
parent.setName("parent");

Loop child = new Loop();
child.setName("child");
child.setOtherLoop(parent);

ObjectMapper mapper = new ObjectMapper();
System.out.println(mapper.writeValueAsString(child));

JsonSchema jsonSchema = mapper.generateJsonSchema(Loop.class);
System.out.println(mapper.writeValueAsString(jsonSchema));
}
}

当我使用 Jackson 2 运行它时,它进入了无限循环

{"name":"child","otherLoop":{"name":"parent"}}
Exception in thread "main" java.lang.StackOverflowError
at com.fasterxml.jackson.databind.cfg.MapperConfig.isEnabled(MapperConfig.java:106)
at com.fasterxml.jackson.databind.SerializationConfig.getAnnotationIntrospector(SerializationConfig.java:382)
at com.fasterxml.jackson.databind.SerializerProvider.getAnnotationIntrospector(SerializerProvider.java:307)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.createContextual(BeanSerializerBase.java:318)
at com.fasterxml.jackson.databind.SerializerProvider._handleContextual(SerializerProvider.java:971)
at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:447)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.getSchema(BeanSerializerBase.java:619)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.getSchema(BeanSerializerBase.java:621)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.getSchema(BeanSerializerBase.java:621)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.getSchema(BeanSerializerBase.java:621)

任何想法,解决方法?!

最佳答案

@XmlAccessorType(XmlAccessType.FIELD)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class Loop {
@XMLAttribute(name = "Name")
private String name;
@XMLAttribute(name = "Loops")
private List<Loop> listLoop;

//getters and setters
}

关于java - 使用 Jackson 生成 JSON 模式进入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12055285/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com