2010-09-20 68 views
0

我已經編寫了使用Jersey庫調用Rest API的Java代碼。 我的第一個顯示所有博客的方法我已經寫的在調用Apis時附加參數

return webResource.path(ConfigurationUtil.LIST_BLOGS).header(ConfigurationUtil.AUTHENTICATION_HEADER, authentication) 
     .accept(MediaType.APPLICATION_XML_TYPE).get(new GenericType<List<CommunityBean>>() { 
    }); 

代碼列出了所有的博客。作爲我LIST_BLOGS串像

public static final String LIST_BLOGS = "api/blogs.xml"; 

它工作正常..

現在我試圖編寫一個代碼,我只想提取2個博客而不是全部

所以我的網址就像

public static final String LIST_BLOGS = "api/blogs.xml?limit=2"; 

由於我無法從封裝文件發送參數ConfigurationUtil文件,我使用的方式

public List<BlogBean> searchBlogsXml(String limit) { 

    final String SEARCH_BLOGS="api/blogs.xml?limit="+limit; 

return webResource.path(SEARCH_BLOGS).header(ConfigurationUtil.AUTHENTICATION_HEADER, authentication) 
    .accept(MediaType.APPLICATION_XML_TYPE).get(new GenericType<List<BlogBean>>() { 
    }); 
} 

當我用像上面我得到406錯誤..

爲什麼這麼如何避免這種情況? 請給出建議..

回答

1

你可以附加一個查詢參數像這樣;

resource.queryParam("limit", 2).get(MyObject.class); 
+0

在那裏我應該給上面的行... – useranon 2010-09-20 08:55:38

+0

@Aruna queryParam是'WebResource'的方法,參考javadocs https://jersey.dev.java.net/nonav/apidocs/latest /jersey/com/sun/jersey/api/client/WebResource.html#queryParam(java.lang.String,%20java.lang.String) – Qwerky 2010-09-20 09:00:01

+0

謝謝.. DOne it .. – useranon 2010-09-20 09:14:10