I am using Jersey and have exposed a resource Resource
which implements an Interface
. One of the methods from Interface
has a parameter a
of type A
which is an abstract class.
Here is some code for explanation:
//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
}
This leads to an error:
JsonMappingException: Can not construct instance of A, problem: abstract types can only be instantiated with additional type information
How can this be avoided/ overcome?
One solution would be to make Jersey/Jackson aware that it can use the concrete implementation of A
(which is AImpl
) in method setA()
of Resource
. Is there any annotation that I can use to do that?
Have you considered simply making Interface
generic? Something like
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) {
...
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.