2013-08-20 56 views
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时,我必须设置一个用户标识和密码,那是什么在原始请求的授权部分?我应该用什么来加密它?

+0

Juned的回答帮我解决这个问题。我还必须将我所有的webresource调用放在同一行上,就像我在做的那样,我只是在头文件webResource.type(MediaType.APPLICATION_XML).post(GetHistoryResponse.class)中设置最后一行, – Cowhunter

回答

3

取景认证头

当你的REST服务使用基本身份验证,这样你就可以使用这一套球衣其余客户端身份验证:

client.addFilter(new HTTPBasicAuthFilter(username, password));