如何在 Jersey/Jackson 中为抽象方法参数指定具体类型?

How to specify concrete type for abstract method param in Jersey/ Jackson?

我正在使用 Jersey 并公开了实现 Interface 的资源 ResourceInterface 中的一种方法有一个类型为 A 的参数 a,它是一个抽象 class.

这里有一些代码来解释:

//Interface.java
public interface Interface {
    public void setA(A a);
}
//Resource.java
@Path("/hello")
public class Resource implements Interface {
    @POST
    public void setA(A a){ //Here I want to specify AImpl instead of A
        //Code that uses AImpl
    }
}
//A.java
public abstract class A{
    //Some abstract stuff
}
//AImpl.java
public class AImpl extends A{
    //Some concrete stuff
}

这会导致错误:

JsonMappingException: Can not construct instance of A, problem: abstract types can only be instantiated with additional type information

如何避免/克服这种情况?

一个解决方案是让 Jersey/Jackson 知道它可以在 [=12] 的方法 setA() 中使用 A(即 AImpl)的具体实现=].我可以使用任何注释来做到这一点吗?

您是否考虑过简单地使 Interface 通用?像

public abstract class SuperType {}

public class SubType extends SuperType {}

public interface Resource<T extends SuperType> {
    Response doSomething(T type);
}

@Path("resource")
public class SubTypeResource implements Resource<SubType> {
    @POST
    @Override
    public Response doSomething(SubType type) {
        ...
    } 
}