我java ee 7在Glassfish 4.1.1. 编写了一个休息服务C#,它返回一个 Book 实例的集合。其余客户端发送获取请求的代码:
public List<Bandwcorpus> downloadBandWCorpus(String corpus) {
Client restClient = ClientBuilder.newClient();
List<Bandwcorpus> ebooks=restClient
.target("http://bookandwalk.pl/api/admindocumentlist")
.queryParam("password", "XXXX").queryParam("corpusid", "YYYY")
.request(MediaType.APPLICATION_JSON)
.get(new GenericType<List<Book>>() {});
restClient.close();
return ebooks;
}
Run Code Online (Sandbox Code Playgroud)
Book 类是一个 POJO:
public class Book {
private static final long serialVersionUID = 1L;
private Integer documentid;
private String corpusid;
private String corpusoriginalid;
private float nett;
private String currencyid;
private int vat;
private Discount discount;
private Upoluj isbn;
public Bandwcorpus() {
}
}
Run Code Online (Sandbox Code Playgroud)
我应用的 get 函数基于http://www.adam-bien.com/roller/abien/entry/jax_rs_returning_a_list博客文章,该解决方案也应用于 Java ee 7 教程。
不幸的是,在这种情况下,我收到以下编译错误:
error: no suitable method found for get(<anonymous com.fasterxml.classmate.GenericType<List<Book>>>)
List<Book> ebooks=restClient.target("http://bookandwalk.pl/api/admindocumentlist").queryParam("password", "XXXXX").queryParam("corpusid", "YYYYY").request(MediaType.APPLICATION_JSON).get(new GenericType<List<Book>>() {});
method SyncInvoker.<T#1>get(Class<T#1>) is not applicable
(cannot infer type-variable(s) T#1
(argument mismatch; <anonymous com.fasterxml.classmate.GenericType<List<Bandwcorpus>>> cannot be converted to Class<T#1>))
method SyncInvoker.<T#2>get(javax.ws.rs.core.GenericType<T#2>) is not applicable
(cannot infer type-variable(s) T#2
(argument mismatch; <anonymous com.fasterxml.classmate.GenericType<List<Book>>> cannot be converted to javax.ws.rs.core.GenericType<T#2>))
Run Code Online (Sandbox Code Playgroud)
其中 T#1,T#2 是类型变量:
T#1 扩展方法中声明的对象 <T#1>get(Class<T#1>)
T#2 扩展方法中声明的对象
<T#2>get(javax.ws.rs.core.GenericType<T#2>)
如果这是 Oracle 和其他专家建议的方式,我知道我做错了什么吗?
最后我找到了解决方案:
List<Book> e=restClient
.target("http://bookandwalk.pl/api/admindocumentlist")
.queryParam("password", "XXXXXX")
.queryParam("corpusid_or_languagecode", "Example")
.request(MediaType.APPLICATION_JSON)
.get(new GenericType<List<Book>> () {});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4949 次 |
| 最近记录: |