我创建了一个javafx客户端,通过Jersey请求Web服务。
这是ws代码:
public List<Document> findDocuments(String query, int row, int start) {
String serviceUri = "/articleservices/searchservice/fulltext";
String queryParams = "/" + query + "/" + row + "/" + start;
String queryUrl = StringUtils.join(new String[]{
AppProperties.SEARCH_WS_BASE_ADDRESS, serviceUri, queryParams});
log.debug("queryUrl: {}", queryUrl);
SearchResult searchResult = new SearchResult();
try {
Client client = Client.create();
WebResource webResource = client.resource(queryUrl);
ClientResponse response = webResource.
accept(MediaType.APPLICATION_XML).
get(ClientResponse.class);
if (response.getStatus() == 200) {
searchResult = response.getEntity(SearchResult.class);
log.debug("NbResults: {}", searchResult.getNbResultItem());
} else {
log.debug("Error");
}
} catch (Exception e) {
log.debug(e.getMessage());
}
DocumentFactory docFactory = new DocumentFactory();
return docFactory.createDocuments(searchResult);
}
我在pom.xml中声明了这些依赖项:
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-client</artifactId>
<version>1.17.1</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-json</artifactId>
<version>1.17.1</version>
</dependency>
当我执行单元测试时它工作正常,我得到“NbResults”结果。 但是使用javafx客户端,查询返回状态200,响应Content-Type是application / xml。但它不会映射实体和WS结果。我收到这个错误:
AM com.sun.jersey.api.client.ClientResponse getEntity
Grave: A message body reader for Java class com.mycompany.search.beans.SearchResult, and Java type class com.mycompany.search.beans.SearchResult, and MIME media type application/xml was not found
Grave: The registered message body readers compatible with the MIME media type are:
*/* ->
com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
请帮忙吗?
== UPDATE ==
SearchResult类看起来像这样!
@XmlRootElement(name = "SEARCH-RESULT")
public class SearchResult implements Serializable {
private static final long serialVersionUID = 87551L;
// Nombre de résultat trouvé
private long nbResultItem;
// Liste contenant le format pivot de la bcp
private List<SearchResultItem> resultItems;
@XmlElement(name = "DOC-NUMBER")
public long getNbResultItem() {
return nbResultItem;
}
public void setNbResultItem(long nbResultItem) {
this.nbResultItem = nbResultItem;
}
@XmlElement(name = "DOC-RESULTS")
public List<SearchResultItem> getResultItems() {
return resultItems;
}
public void setResultItems(List<SearchResultItem> resultItems) {
this.resultItems = resultItems;
}
}
答案 0 :(得分:0)
最后,我使用了org.springframework.web.client.RestTemplate。它完美无缺! http://spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate