JsonMappingException: Type id handling not implemented, with enableDefaultTyping and custom serializer

空扰寡人 提交于 2019-12-11 15:54:21

问题


I use Java 9, Jackson-core and jackson-databind 2.5.5 I would like to use custom serialization with the DefaultTyping.NON_FINAL option for writing class names in Json.

if I delete the default typing NON_FINAL, everything works.

When I add the NON_FINAL option, my custom serializer "MySerializer" is called and I have the exception JsonMappingException: Type id handling not implemented

public class Main {
    public static void main(String[] args) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enableDefaultTyping();
        objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);

        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(new MySerializer());

        objectMapper.registerModule(simpleModule);

        System.out.println(objectMapper.writeValueAsString(new MyObject(1)));
    }
}

public class MyObject {
    private int a = 0;

    public MyObject() {
    }

    public MyObject(int a) {
        this.a = a;
    }

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }
}

public class MySerializer extends StdSerializer<MyObject> {
    protected MySerializer() {
        super(MyObject.class);
    }

    public void serialize(MyObject myObject, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartObject();
        jsonGenerator.writeNumberField("newNameForFieldA", myObject.getA());
        jsonGenerator.writeEndObject();
    }
}


    <dependencies>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.5</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.5</version>
        </dependency>
    </dependencies>

the result without NON_FINAL default typing :

{"newNameForFieldA":1}

the exception :

Exception in thread "main" com.fasterxml.jackson.databind.JsonMappingException: Type id handling not implemented for type com.test.jackson.main2.MyObject (by serializer of type com.test.jackson.main2.MySerializer) at com.fasterxml.jackson.databind.SerializerProvider.mappingException(SerializerProvider.java:1047) at com.fasterxml.jackson.databind.JsonSerializer.serializeWithType(JsonSerializer.java:142) at com.fasterxml.jackson.databind.ser.impl.TypeWrappedSerializer.serialize(TypeWrappedSerializer.java:32) at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:129) at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3387) at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2781) at com.test.jackson.main2.Main.main(Main.java:18)

来源:https://stackoverflow.com/questions/56971020/jsonmappingexception-type-id-handling-not-implemented-with-enabledefaulttyping

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!