我有一个通用的JAX-RS资源类,我已经定义了一个泛型findAll方法
public abstract class GenericDataResource<T extends GenericModel> {
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response findAll() {
Query query = em.createNamedQuery(modelClass.getSimpleName()+".findAll");
List<T> list = query.getResultList();
return Response.ok(new GenericEntity<List<T>>(list) {}).build();
}
}
Run Code Online (Sandbox Code Playgroud)
和用户类:
public class User extends GenericModel {
...
}
Run Code Online (Sandbox Code Playgroud)
这里是示例子类定义:
@Path("users")
public class UserResource extends GenericDataResource<User> {
public UserResource() {
super(User.class);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:
com.sun.jersey.api.MessageException: A message body writer for Java class
java.util.Vector, and Java type java.util.List<T>,
and MIME media type application/json was not found exception.
Run Code Online (Sandbox Code Playgroud)
如果我用定义的类替换T,例如User:
GenericEntity<List<User>>(list) …
我有一个A类,看起来如下:
public class A {
// there is a public API that uses both
// method1 and method2, but that's not important
// in this question.
public Integer publicApi(Integer a, Integer b) {
// a bunch of other stuff is done that is
// not related to method1 or method2 here.
// However, method1 IS used somewhere in here.
// For now, I don't want to test other
// implementation details for this API, I only
// am interested in …Run Code Online (Sandbox Code Playgroud)