在Custom Class中使用Jackson的ObjectMapper

时间:2016-11-22 22:29:07

标签: java json jackson

我有一个类如:

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

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

但是,当我调用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)

无论如何我可以在类本身内部调用Jackson的ObjectMapper,以便我可以更好地将Json String与对象本身联系起来吗?

编辑:我忘了提到我在这里使用lombok的数据,因此不需要每个属性的setter和getter。

3 个答案:

答案 0 :(得分:2)

当然,但是你需要让它忽略jsonString属性,以避免无休止的递归调用。将@JsonIgnore添加到getter。或者命名您的方法,使其不再是属性,例如toJson()(这也是一个更好的名称,IMO)。

也就是说,每次创建一个新的ObjectMap都不是一个好主意。相反,你应该重复使用同一个,一劳永逸地构建。

答案 1 :(得分:1)

您需要执行以下操作:

  • 写getter和setter
  • 使用getJsonString
  • 注释@JsonIgnore

下面应该有效:

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());
    }
}

答案 2 :(得分:1)

将您的getJsonString()重命名为toJsonString()

ObjectMapper认为你的bean中有一个属性jsonString。它试图通过getter调用它。这导致无休止的呼唤。