小编Nis*_*kar的帖子

通过GenericEntity <List <T >>在RESTful Response对象中使用Java-generics模板类型

我有一个通用的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) …

java generics rest jax-rs jersey

14
推荐指数
2
解决办法
3万
查看次数

测试私有静态方法,该方法在Java内部使用另一个私有静态方法

我有一个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)

java junit powermockito

3
推荐指数
1
解决办法
4281
查看次数

标签 统计

java ×2

generics ×1

jax-rs ×1

jersey ×1

junit ×1

powermockito ×1

rest ×1