获取REST资源作为列表< T>与泽西岛 [英] Fetching REST resource as List<T> with Jersey

查看:150
本文介绍了获取REST资源作为列表< T>与泽西岛的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Jersey中编写一个通用函数,它可以用来通过REST获取相同类型的对象列表。我将其基于本论坛中的信息:链接

  @Override 
public< T>列表与LT; T> fetchResourceAsList(String url){
ClientConfig cc = new DefaultClientConfig();
客户端c = Client.create(cc);
if(userName!= null&& password!= null){
c.addFilter(new HTTPBasicAuthFilter(userName,password));
}
WebResource resource = c.resource(url);
return resource.get(new GenericType< List< T>>(){});
}

然而这不起作用。如果我尝试执行它,我会得到以下错误: SEVERE:Java类java.util.List和Java类型java.util.List< T>和MIME媒体类型的消息正文阅读器没有找到application / xml



然而,如果我没有模板编写这个函数(用一个实际的类名替换T),它可以正常工作。当然,这种方式失去了它的意义。

有没有办法解决这个问题?

解决方案

我找到了解决方案
https://java.net/projects/jersey/lists/users/archive/2011-08/message/37

  public< T>列表与LT; T> getAll(final Class< T> clazz){

ParameterizedType parameterizedGenericType = new ParameterizedType(){
public Type [] getActualTypeArguments(){
return new Type [] {clazz} ;
}

public Type getRawType(){
return List.class;
}

public Type getOwnerType(){
return List.class;
}
};

GenericType< List< T>> genericType = new GenericType< List< T>>(
parameterizedGenericType){
};

return service.path(Path.ROOT).path(clazz.getSimpleName())
.accept(MediaType.APPLICATION_XML).get(genericType);
}


I'm trying to write a generic function in Jersey which can be used to fetch a List of objects of the same type through REST. I based it on the informations found in this forum: link

@Override
public <T> List<T> fetchResourceAsList(String url) {
  ClientConfig cc = new DefaultClientConfig();
  Client c = Client.create(cc);
  if (userName!=null && password!=null) {
    c.addFilter(new HTTPBasicAuthFilter(userName, password)); 
  }
  WebResource resource = c.resource(url);
  return resource.get(new GenericType<List<T>>() {});
}

However this is not working. If i try to execute it, i get the following error: SEVERE: A message body reader for Java class java.util.List, and Java type java.util.List<T>, and MIME media type application/xml was not found.

However if i write this function without templating (replacing T with an actual class name) it just works fine. Of course this way the function loses it's meaning.

Is there a way to fix this?

解决方案

I've found solution https://java.net/projects/jersey/lists/users/archive/2011-08/message/37

public <T> List<T> getAll(final Class<T> clazz) {

    ParameterizedType parameterizedGenericType = new ParameterizedType() {
        public Type[] getActualTypeArguments() {
            return new Type[] { clazz };
        }

        public Type getRawType() {
            return List.class;
        }

        public Type getOwnerType() {
            return List.class;
        }
    };

    GenericType<List<T>> genericType = new GenericType<List<T>>(
            parameterizedGenericType) {
    };

    return service.path(Path.ROOT).path(clazz.getSimpleName())
            .accept(MediaType.APPLICATION_XML).get(genericType);
}

这篇关于获取REST资源作为列表&lt; T&gt;与泽西岛的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆