Jackson MixInAnnotation using SimpleModule doesn't work

I am using SimpleModule to register MixIn with serialization and deserialization. I cannot get it to work. The classes are shown below. When I print the serialized string, I see the size printed and the properties are not named as I specified in the mixin. This is a seal {"w":5,"h":10,"size":50}

. So registering the connection with the serializer and deserialization configuration 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;
    }
}

      

MixIn registration:

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

    }
}

      

+2


source to share


2 answers


I can't see where you registered your module MyModule

anywhere? Jackson won't pick him up unless you tell him which module to use. You tried:

objectMapper.registerModule(new MyModule());



in your test (right after instantiating ObjectMapper)? The modules defining the mixing work well for me.

Of course, if you are only registering a Mix-Ins pair and not doing any other configuration, using the method is addMixInAnnotations()

much easier.

+3


source


Use this approach instead:

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

      



I gave a similar answer here , and the other person stated in the comments that the module example (from here ) didn't work for him either.

+2


source







All Articles