杰克逊MixInAnnotation使用SimpleModule不起作用 [英] Jackson MixInAnnotation using SimpleModule does not work
问题描述
我使用SimpleModule与序列化和反序列化注册混入。我无法使它工作。类如下所示。当我打印序列化的字符串我看到大小的印刷和我在混入指定的属性没有命名。它是印刷 {W:5,H:10,大小:50}
。因此,混合 - 与两个串行器和反序列化的配置注册不成功。我在做什么错了。
混合类:
进口组织codehaus.jackson.annotate.JsonIgnore。
。进口组织codehaus.jackson.annotate.JsonProperty; 抽象类混入{
混入(@JsonProperty(宽度)INT W,@JsonProperty(高),INT高){
} @JsonProperty(宽度)
抽象INT getW(); @JsonProperty(高)
抽象INT GETH(); @JsonIgnore
抽象INT的getSize(); }
Rectangle类:
公共final类矩形{
最终私人诠释W,H; 公共矩形(INT W,INT高){
this.w = W;
this.h = H;
} 公众诠释getW(){
返回瓦;
} 公众诠释GETH(){
返回H;
} 公众诠释的getSize(){
返回W * H;
}
}
注册MIXIN:
进口组织codehaus.jackson.Version。
。进口组织codehaus.jackson.map.module.SimpleModule;
公共类MyModule的扩展SimpleModule {
公共MyModule的(){
超(模块名,新的版本(0,0,1,NULL));
} @覆盖
公共无效setupModule(SetupContext上下文){
context.setMixInAnnotations(Rectangle.class,MixIn.class); //和其他设置,如果有的话
}
}
测试类:
进口java.io.IOException异常;。进口组织codehaus.jackson.map.ObjectMapper;
进口org.junit.Test;公共类DeserializationTest { @测试
公共无效测试()抛出IOException ObjectMapper objectMapper =新ObjectMapper(); // objectMapper.getSerializationConfig()addMixInAnnotations(Rectangle.class,MixIn.class);
// objectMapper.getDeserializationConfig()addMixInAnnotations(Rectangle.class,MixIn.class); 字符串str = objectMapper.writeValueAsString(新的Rectangle(5,10));
的System.out.println(STR);
矩形R = objectMapper.readValue(STR,Rectangle.class); }
}
我不明白,你已经注册你的模块 MyModule的
地方?除非你告诉它有使用模块杰克逊将不会把它捡起来。你是否尝试过做:
objectMapper.registerModule(新MyModule的());
在您的测试(右后实例化ObjectMapper)?模块定义搭配的插件为我工作得很好。
当然,如果你只是注册了几个驴友造访并没有做其他的配置,使用 addMixInAnnotations()
方法要容易得多。
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);
}
}
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.
这篇关于杰克逊MixInAnnotation使用SimpleModule不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!