Jackson MixInAnnotation 使用 SimpleModule 不起作用 [英] Jackson MixInAnnotation using SimpleModule does not work

查看:64
本文介绍了Jackson MixInAnnotation 使用 SimpleModule 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SimpleModule 注册带有序列化和反序列化的 MixIn.我无法让它工作.类如下所示.当我打印序列化字符串时,我看到打印的大小和属性没有按照我在 mixin 中指定的方式命名.它正在打印 {"w":5,"h":10,"size":50}.因此,同时使用序列化器和反序列化配置进行混合注册是不成功的.我做错了什么.

I'm using SimpleModule to register the MixIn with Serialization and Deserialization. I am unable to make it work. The classes are shown below. WHen I print the serialized string I see the size printed and properties are not named as I specified in the mixin. It is printing {"w":5,"h":10,"size":50}. So mix-in registration with both serializer and deserialization configurations was unsuccessful. What am I doing wrong.

混入类:

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;

    abstract class MixIn {
        MixIn(@JsonProperty("width") int w, @JsonProperty("height") int h) {
        }

        @JsonProperty("width")
        abstract int getW();

        @JsonProperty("height")
        abstract int getH();

        @JsonIgnore
        abstract int getSize();

    }

矩形类:

 public final class Rectangle {
    final private int w, h;

    public Rectangle(int w, int h) {
        this.w = w;
        this.h = h;
    }

    public int getW() {
        return w;
    }

    public int getH() {
        return h;
    }

    public int getSize() {
        return w * h;
    }
}

注册 MixIn:

import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.module.SimpleModule;


public class MyModule extends SimpleModule {
    public MyModule() {
        super("ModuleName", new Version(0, 0, 1, null));
    }

    @Override
    public void setupModule(SetupContext context) {
        context.setMixInAnnotations(Rectangle.class, MixIn.class);

        // and other set up, if any
    }
}

测试类:

import java.io.IOException;

import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;

public class DeserializationTest {

    @Test
    public void test() throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();

        // objectMapper.getSerializationConfig().addMixInAnnotations(Rectangle.class, MixIn.class);
        // objectMapper.getDeserializationConfig().addMixInAnnotations(Rectangle.class, MixIn.class);

        String str = objectMapper.writeValueAsString(new Rectangle(5, 10));
        System.out.println(str);
        Rectangle r = objectMapper.readValue(str, Rectangle.class);

    }
}

推荐答案

我没有看到您在何处注册了您的模块 MyModule?除非你告诉它有一个模块可以使用,否则杰克逊不会拿起它.你有没有试过:

I don't see where you've registered your module MyModule anywhere? Jackson won't pick it up unless you tell it there's a module to use. Have you tried doing:

objectMapper.registerModule(new MyModule());

在您的测试中(在您实例化 ObjectMapper 之后)?定义混入的模块对我来说效果很好.

in your test (right after you instantiate the ObjectMapper)? Modules defining mix-ins work well for me.

当然,如果您只注册几个 Mix-In 而没有进行其他配置,使用 addMixInAnnotations() 方法会容易得多.

Of course, if you're only registering a couple of Mix-Ins and not doing other configuration, using the addMixInAnnotations() method is much easier.

这篇关于Jackson MixInAnnotation 使用 SimpleModule 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆