简体   繁体   English

在自定义类中使用Jackson的ObjectMapper

[英]Using Jackson's ObjectMapper inside Custom Class

I have a class such as: 我有一个像这样的课程:

@Data
public class Lmao {
   String lol;
   String lel;

   public String getJsonString() throws Exception {
       ObjectMapper objectMapper = new ObjectMapper();
       return objectMapper.writeValueAsString(this);
   }
}

However, when I invoke the getJsonString() method, I get the following error: 但是,当我调用getJsonString()方法时,出现以下错误:

java.lang.StackOverflowError
    at com.fasterxml.jackson.databind.introspect.AnnotatedClass._findFields(AnnotatedClass.java:795)
    at com.fasterxml.jackson.databind.introspect.AnnotatedClass._findFields(AnnotatedClass.java:802)
    at com.fasterxml.jackson.databind.introspect.AnnotatedClass.resolveFields(AnnotatedClass.java:571)
    at com.fasterxml.jackson.databind.introspect.AnnotatedClass.fields(AnnotatedClass.java:353)
    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector._addFields(POJOPropertiesCollector.java:350)
    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.collectAll(POJOPropertiesCollector.java:283)
    at com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector.getJsonValueMethod(POJOPropertiesCollector.java:169)
    at com.fasterxml.jackson.databind.introspect.BasicBeanDescription.findJsonValueMethod(BasicBeanDescription.java:223)
    at com.fasterxml.jackson.databind.ser.BasicSerializerFactory.findSerializerByAnnotations(BasicSerializerFactory.java:349)
    at com.fasterxml.jackson.databind.ser.BeanSerializerFactory._createSerializer2(BeanSerializerFactory.java:208)
    at com.fasterxml.jackson.databind.ser.BeanSerializerFactory.createSerializer(BeanSerializerFactory.java:157)
    at com.fasterxml.jackson.databind.SerializerProvider._createUntypedSerializer(SerializerProvider.java:1215)
    at com.fasterxml.jackson.databind.SerializerProvider._createAndCacheUntypedSerializer(SerializerProvider.java:1167)
    at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:490)
    at com.fasterxml.jackson.databind.SerializerProvider.findTypedValueSerializer(SerializerProvider.java:688)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:107)
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3613)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2980)

Is there anyway I can call Jackson's ObjectMapper inside the class itself so that I can better tie in the Json String with the object itself? 无论如何,我可以在类本身内部调用Jackson的ObjectMapper,以便更好地将Json String与对象本身联系起来吗?

Edit: I forgot to mention that I am using lombok's data here, so the setters and getters for each individual property isnt needed. 编辑:我忘了提到我在这里使用lombok的数据,因此不需要每个属性的设置器和获取器。

Sure, but you need to make it ignore the jsonString property, to avoid that endless recursive call. 可以,但是您需要使其忽略jsonString属性,以避免无休止的递归调用。 Add @JsonIgnore to the getter. @JsonIgnore添加到getter。 Or name your method so that it isn't a property anymore, like for example toJson() (which is also a better name, IMO). 或命名您的方法,使其不再是属性,例如toJson() (这也是一个更好的名称,IMO)。

That said, creating a new ObjectMapper everytime is not a good idea. 也就是说,每次都创建一个新的ObjectMapper并不是一个好主意。 You should instead alsays reuse the same one, constructed once and for all. 相反,您应该重新使用一劳永逸地构造的同一个。

You need to do the following: 您需要执行以下操作:

  • Write getter and setters 编写getter和setter
  • Annotate getJsonString with @JsonIgnore 注释getJsonString@JsonIgnore

Below should work: 下面应该工作:

public class Test {

    String lol;
    String lel;

    @JsonIgnore
    public String getJsonString() throws Exception {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(this);
    }

    public String getLol() {
        return lol;
    }


    public void setLol(String lol) {
        this.lol = lol;
    }


    public String getLel() {
        return lel;
    }


    public void setLel(String lel) {
        this.lel = lel;
    }

    public static void main(String[] args) throws Exception{
        Test t = new Test();
        System.out.println(t.getJsonString());
    }
}

rename your getJsonString() to toJsonString() . 将您的getJsonString()重命名为toJsonString()

ObjectMapper thinks there is an property jsonString in your bean. ObjectMapper认为您的bean中有一个属性jsonString It try to call it via getter. 它尝试通过getter调用它。 This results in endless call. 这导致无休止的通话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM