简体   繁体   English

Jersey客户端调用RESTful服务

[英]Jersey client calling RESTful service

I am new to using Jersey and I am struggling to get my client working against an existing RESTful service. 我是使用Jersey的新手,我正在努力让我的客户端对抗现有的RESTful服务。 I have a SOAPUI call that works, here is the raw data from that request. 我有一个可用的SOAPUI调用,这是来自该请求的原始数据。

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"}
    ]
    }

}

At this point I am willing to hard code all the values in just to see it work. 在这一点上,我愿意硬编码所有的价值观,只是为了看它是否有效。 But here is my client code. 但这是我的客户端代码。

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();
}

When I use SOAPUI I have to set a userid and password, is that what is in the authorization part of the raw request? 当我使用SOAPUI时,我必须设置用户ID和密码,那是原始请求的授权部分中的内容吗? What should I use to encrypt it? 我应该用什么来加密它?

Framing Authentication Header 框架认证标头

As your REST service is using Basic authentication, so you can set the jersey rest client authentication using this: 由于您的REST服务使用的是基本身份验证,因此您可以使用以下命令设置jersey rest客户端身份验证:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM