Jackson MixInAnnotation using SimpleModule does not work

徘徊边缘 提交于 2019-12-11 03:15:22

问题


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.

MixIn Class:

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

    }

Rectangle Class:

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

Registering 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
    }
}

Test Class:

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

    }
}

回答1:


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

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

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




回答2:


Use this approach instead:

ObjectMapper mapper = new ObjectMapper();
mapper.addMixInAnnotations(Rectangle.class, Mixin.class);

I gave a similar answer here and the questioner stated in the comments that the module example (from here) did not work for him either.



来源:https://stackoverflow.com/questions/15299944/jackson-mixinannotation-using-simplemodule-does-not-work

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