互联网上有很多如何使用RESTful Client API的东西。 这些是基础。 但是,尽管该主题看起来微不足道,但仍然存在一些障碍,尤其是对于初学者而言。
在这篇文章中,我将尝试总结我的专业知识,以及我如何在实际项目中做到这一点。 我通常使用Jersey(用于构建RESTful服务的参考实现)。 参见例如我的另一篇文章 。 在本文中,我将从JSF bean调用真正的远程服务。 让我们编写一个会话范围的bean RestClient。
package com.cc.metadata.jsf.controller.common;import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;import java.io.Serializable;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;/*** This class encapsulates some basic REST client API.*/
@ManagedBean
@SessionScoped
public class RestClient implements Serializable {private transient Client client;public String SERVICE_BASE_URI;@PostConstructprotected void initialize() {FacesContext fc = FacesContext.getCurrentInstance();SERVICE_BASE_URI = fc.getExternalContext().getInitParameter('metadata.serviceBaseURI');client = Client.create();}public WebResource getWebResource(String relativeUrl) {if (client == null) {initialize();}return client.resource(SERVICE_BASE_URI + relativeUrl);}public ClientResponse clientGetResponse(String relativeUrl) {WebResource webResource = client.resource(SERVICE_BASE_URI + relativeUrl);return webResource.accept('application/json').get(ClientResponse.class);}
}
在此类中,我们获得了在web.xml中指定(配置)的服务基础URI。
<context-param><param-name>metadata.serviceBaseURI</param-name><param-value>http://somehost/metadata/</param-value>
</context-param>
此外,我们编写了两种方法来接收远程资源。 我们打算接收JSON格式的资源,并将其转换为Java对象。 下一个bean演示了如何对GET请求执行此任务。 Bean HistoryBean通过使用GsonConverter将接收到的JSON转换为Document对象。 最后两节将不在此处显示(没关系)。 Document是一个简单的POJO,而GsonConverter是一个包装Gson的单例实例。
package com.cc.metadata.jsf.controller.history;import com.cc.metadata.jsf.controller.common.RestClient;
import com.cc.metadata.jsf.util.GsonConverter;
import com.cc.metadata.model.Document;import com.sun.jersey.api.client.ClientResponse;import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;/*** Bean getting history of the last extracted documents.*/
@ManagedBean
@ViewScoped
public class HistoryBean implements Serializable {@ManagedProperty(value = '#{restClient}')private RestClient restClient;private List<Document> documents;private String jsonHistory;public List<Document> getDocuments() {if (documents != null) {return documents;}ClientResponse response = restClient.clientGetResponse('history');if (response.getStatus() != 200) {throw new RuntimeException('Failed service call: HTTP error code : ' + response.getStatus());}// get history as JSONjsonHistory = response.getEntity(String.class);// convert to Java array / list of Document instancesDocument[] docs = GsonConverter.getGson().fromJson(jsonHistory, Document[].class);documents = Arrays.asList(docs);return documents;}// getter / setter...
}
下一个bean演示了如何通过POST与远程服务进行通信。 我们打算发送上传文件的内容。 我使用PrimeFaces的 FileUpload组件,以便可以从侦听器的参数FileUploadEvent中提取内容作为InputStream。 这在这里并不重要,您还可以使用任何其他Web框架来获取文件内容(也可以作为字节数组)。 更重要的是,看看如何处理RESTful Client类FormDataMultiPart和FormDataBodyPart。
package com.cc.metadata.jsf.controller.extract;import com.cc.metadata.jsf.controller.common.RestClient;
import com.cc.metadata.jsf.util.GsonConverter;
import com.cc.metadata.model.Document;import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataBodyPart;
import com.sun.jersey.multipart.FormDataMultiPart;import org.primefaces.event.FileUploadEvent;import java.io.IOException;
import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;import javax.ws.rs.core.MediaType;/*** Bean for extracting document properties (metadata).*/
@ManagedBean
@ViewScoped
public class ExtractBean implements Serializable {@ManagedProperty(value = '#{restClient}')private RestClient restClient;private String path;public void handleFileUpload(FileUploadEvent event) throws IOException {String fileName = event.getFile().getFileName();FormDataMultiPart fdmp = new FormDataMultiPart();FormDataBodyPart fdbp = new FormDataBodyPart(FormDataContentDisposition.name('file').fileName(fileName).build(),event.getFile().getInputstream(), MediaType.APPLICATION_OCTET_STREAM_TYPE);fdmp.bodyPart(fdbp);WebResource resource = restClient.getWebResource('extract');ClientResponse response = resource.accept('application/json').type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, fdmp);if (response.getStatus() != 200) {throw new RuntimeException('Failed service call: HTTP error code : ' + response.getStatus());}// get extracted document as JSONString jsonExtract = response.getEntity(String.class);// convert to Document instanceDocument doc = GsonConverter.getGson().fromJson(jsonExtract, Document.class);...}// getter / setter...
}
最后但并非最不重要的一点,我想演示如何使用任何查询字符串(URL参数)发送GET请求。 下一个方法通过看起来像http:// somehost / metadata / extract?file = <some file path>的URL询问远程服务。
public void extractFile() {WebResource resource = restClient.getWebResource('extract');ClientResponse response = resource.queryParam('file', path).accept('application/json').get(ClientResponse.class);if (response.getStatus() != 200) {throw new RuntimeException('Failed service call: HTTP error code : ' + response.getStatus());}// get extracted document as JSONString jsonExtract = response.getEntity(String.class);// convert to Document instanceDocument doc = GsonConverter.getGson().fromJson(jsonExtract, Document.class);...
}
参考:在我们的软件开发博客上,来自我们JCG合作伙伴 Oleg Varaksin的RESTful Client API进行GET / POST 。
翻译自: https://www.javacodegeeks.com/2013/01/get-post-with-restful-client-api.html