Java 如何使用jersey和MOxy封送和解封JAX-RS中的抽象类
嗨,我想将jersey 1.x更新为2.x,将Jackson更新为MOxy。 但我不知道如何在MOxy中封送和解封抽象类 在Jackson中,它完全使用自定义解析器工作。 这是我的示例代码 豆子 分解器Java 如何使用jersey和MOxy封送和解封JAX-RS中的抽象类,java,json,rest,jersey,moxy,Java,Json,Rest,Jersey,Moxy,嗨,我想将jersey 1.x更新为2.x,将Jackson更新为MOxy。 但我不知道如何在MOxy中封送和解封抽象类 在Jackson中,它完全使用自定义解析器工作。 这是我的示例代码 豆子 分解器 public class AnimalResolver extends TypeIdResolverBase{ .... @Override public String idFromValueAndType(Object context, Class<?> s
public class AnimalResolver extends TypeIdResolverBase{
....
@Override
public String idFromValueAndType(Object context, Class<?> suggestedType) {
if (Dog.class.isAssignableFrom(suggestedType)) {
return "dog";
} else if (Cat.class.isAssignableFrom(suggestedType)) {
return "cat";
}
throw new IllegalArgumentException();
}
@Override
public JavaType typeFromId(DatabindContext context, String id) throws IOException {
TypeFactory typeFactory = (context != null) ? context.getTypeFactory() : TypeFactory.defaultInstance();
if ("dog".equals(id)) {
return typeFactory.constructType(Dog.class);
} else if ("cat".equals(id)) {
return typeFactory.constructType(Cat.class);
}
throw new IllegalArgumentException();
}
}
公共类AnimalResolver扩展了TypeIdResolverBase{
....
@凌驾
公共字符串idFromValueAndType(对象上下文,类suggestedType){
if(Dog.class.isAssignableFrom(suggestedType)){
返回“狗”;
}else if(Cat.class.isAssignableFrom(suggestedType)){
返回“猫”;
}
抛出新的IllegalArgumentException();
}
@凌驾
公共JavaType typeFromId(DatabindContext上下文,字符串id)引发IOException{
TypeFactory类型工厂=(上下文!=null)?context.getTypeFactory():TypeFactory.defaultInstance();
如果(“狗”。等于(id)){
返回类型factory.constructType(Dog.class);
}否则,如果(“猫”等于(id)){
返回类型factory.constructType(类别);
}
抛出新的IllegalArgumentException();
}
}
我如何在MOxy中做到这一点?本打算链接到@xmlsealsothanks,但仍然无法使用我的代码。我的API很抱歉,我无法将代码粘贴到这里…此测试结果为org.junit.ComparisonFailure:expected:but was:和java.lang.AssertionError:200 OK?Expected:is-but:was `您可以为链接到@xmlsealsothanks的模型类添加另一个要点,但仍然无法使用我的代码吗。我的API很抱歉,我无法将代码粘贴到这里…此测试结果为org.junit.ComparisonFailure:expected:but was:和java.lang.AssertionError:200 OK?Expected:is-but:was`你能在模型类中添加另一个要点吗
public class AnimalResolver extends TypeIdResolverBase{
....
@Override
public String idFromValueAndType(Object context, Class<?> suggestedType) {
if (Dog.class.isAssignableFrom(suggestedType)) {
return "dog";
} else if (Cat.class.isAssignableFrom(suggestedType)) {
return "cat";
}
throw new IllegalArgumentException();
}
@Override
public JavaType typeFromId(DatabindContext context, String id) throws IOException {
TypeFactory typeFactory = (context != null) ? context.getTypeFactory() : TypeFactory.defaultInstance();
if ("dog".equals(id)) {
return typeFactory.constructType(Dog.class);
} else if ("cat".equals(id)) {
return typeFactory.constructType(Cat.class);
}
throw new IllegalArgumentException();
}
}