Java Jersey客户端API

时间:2012-08-06 12:11:25

标签: java rest jersey

我是Java Jersey Client API的新手,想要登录服务器,例如:

POST http:/xxxx.net/auth/login

Content-Type: application/json
{"login": "xxxxx", "password": "XXXXX"}

你能给我看一个这样的Java Jersey客户端API代码的例子吗?

1 个答案:

答案 0 :(得分:1)

// create a Jersey client
Client client = Client.create();

// create the JSON as a Map 
Map<String, String> data = new HashMap<String, String>();
data.put("login", "xxxxx");
data.put("password", "xxxxx");

// post to the resource with APPLICATION_JSON_TYPE and it will be converted to JSON for you
client.resource("http://xxxx.net/auth/login")
      .type(MediaType.APPLICATION_JSON_TYPE)
      .post(data);