[英]How to specify concrete type for abstract method param in Jersey/ Jackson?
我正在使用Jersey,并公开了实现Interface
的资源Resource
。 Interface
中的一种方法具有类型A
的参数a
,它是抽象类。
这是一些代码说明:
//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知道它可以在Resource
方法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) {
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.