使用RESTful客户端API进行GET / POST
互聯(lián)網(wǎng)上有很多如何使用RESTful Client API的東西。 這些是基礎(chǔ)。 但是,盡管該主題看起來微不足道,但仍然存在一些障礙,尤其是對于初學(xué)者而言。
在這篇文章中,我將嘗試總結(jié)我的專業(yè)知識,以及我如何在實(shí)際項(xiàng)目中做到這一點(diǎn)。 我通常使用Jersey(用于構(gòu)建RESTful服務(wù)的參考實(shí)現(xiàn))。 參見例如我的另一篇文章 。 在本文中,我將從JSF bean調(diào)用真正的遠(yuǎn)程服務(wù)。 讓我們編寫一個(gè)會(huì)話范圍的bean RestClient。
在此類中,我們獲得了在web.xml中指定(配置)的服務(wù)基礎(chǔ)URI。
<context-param><param-name>metadata.serviceBaseURI</param-name><param-value>http://somehost/metadata/</param-value> </context-param>此外,我們編寫了兩種方法來接收遠(yuǎn)程資源。 我們打算接收J(rèn)SON格式的資源,并將其轉(zhuǎn)換為Java對象。 下一個(gè)bean演示了如何對GET請求執(zhí)行此任務(wù)。 Bean HistoryBean通過使用GsonConverter將接收到的JSON轉(zhuǎn)換為Document對象。 最后兩節(jié)將不在此處顯示(沒關(guān)系)。 Document是一個(gè)簡單的POJO,而GsonConverter是一個(gè)包裝Gson的單例實(shí)例。
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... }下一個(gè)bean演示了如何通過POST與遠(yuǎn)程服務(wù)進(jìn)行通信。 我們打算發(fā)送上傳文件的內(nèi)容。 我使用PrimeFaces的 FileUpload組件,以便可以從偵聽器的參數(shù)FileUploadEvent中提取內(nèi)容作為InputStream。 這在這里并不重要,您還可以使用任何其他Web框架來獲取文件內(nèi)容(也可以作為字節(jié)數(shù)組)。 更重要的是,看看如何處理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... }最后但并非最不重要的一點(diǎn),我想演示如何使用任何查詢字符串(URL參數(shù))發(fā)送GET請求。 下一個(gè)方法通過看起來像http:// somehost / metadata / extract?file = <some file path>的URL詢問遠(yuǎn)程服務(wù)。
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);... }參考:在我們的軟件開發(fā)博客上,來自我們JCG合作伙伴 Oleg Varaksin的RESTful Client API進(jìn)行GET / POST 。
翻譯自: https://www.javacodegeeks.com/2013/01/get-post-with-restful-client-api.html
總結(jié)
以上是生活随笔為你收集整理的使用RESTful客户端API进行GET / POST的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 罗永浩吐槽:“子公司&rdq
- 下一篇: 科技昨夜今晨 0904:消息称华为将于明