1
我是使用Jersey的新手,我努力讓客戶端對現有的RESTful服務工作。我有一個可用的SOAPUI調用,這裏是來自該請求的原始數據。Jersey客戶端調用RESTful服務
POST http://localhost/QWJB/WENXrest//getHistory HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json
User-Agent: Jakarta Commons-HttpClient/3.1
Content-Length: 189
Authorization: Basic VUNRRzE02fr45tDE0d1RQdw==
Host: localhost
{getHistory:
{
wENXHeader:
{
languageCode: "en"
},
Account: "111111111",
services:
[
{Service: "CE000001"},
{Service: "CE000002"}
]
}
}
在這一點上,我願意硬編碼的所有值只是爲了看到它的工作。但這是我的客戶端代碼。
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.core.MediaType;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
public void MakeRestCall() {
try {
GetHistory requestData = new GetHistory();
requestData.setAccount("111111111");
Client client = Client.create();
WebResource webResource = client
.resource("http://localhost/QWJB/WNEXrest//getHistory");
webResource.accept("application/json");
webResource.entity(requestData);
GetHistoryResponse response =
webResource.type(MediaType.APPLICATION_XML).post(GetHistoryResponse.class);
} catch (Exception e) {
logger.error(message);
e.printStackTrace();
}
當我使用SOAPUI時,我必須設置一個用戶標識和密碼,那是什麼在原始請求的授權部分?我應該用什麼來加密它?
Juned的回答幫我解決這個問題。我還必須將我所有的webresource調用放在同一行上,就像我在做的那樣,我只是在頭文件webResource.type(MediaType.APPLICATION_XML).post(GetHistoryResponse.class)中設置最後一行, – Cowhunter