前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用 Jersey 调用 Restful 服务

使用 Jersey 调用 Restful 服务

作者头像
netkiller old
发布2018-03-05 17:26:51
1.6K0
发布2018-03-05 17:26:51
举报
文章被收录于专栏:NetkillerNetkiller

第 20 章 Jersey - RESTful Web Services in Java.

目录

  • 20.1. Client
    • 20.1.1. Maven 版本
    • 20.1.2. GET 操作
    • 20.1.3. GET + Auth 用户认证

https://jersey.java.net/

20.1. Client

20.1.1. Maven 版本

1.x

代码语言:javascript
复制
			<!-- https://mvnrepository.com/artifact/com.sun.jersey/jersey-client -->
<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.19.2</version>
</dependency>	

2.x 版本

代码语言:javascript
复制
			<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-client</artifactId>
    <version>2.23.2</version>
</dependency>		

两个版本差异非常大,本文的例子使用2.23.2

20.1.2. GET 操作

代码语言:javascript
复制
			package cn.netkiller.jersey;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientConfig;

public class JerseyClientGet {

	public static void main(String[] args) {

		ClientConfig clientConfig = new ClientConfig();

		Client client = ClientBuilder.newClient(clientConfig);
		WebTarget webTarget = client.target("http://inf.netkiller.cn").path("/list/json/2.html");

		Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
		Response response = invocationBuilder.get();

		System.out.println(response.getStatus());
		System.out.println(response.getStatusInfo());

		if (response.getStatus() == 200) {

			String output = response.readEntity(String.class);
			System.out.println(output);

		}

	}
}	

20.1.3. GET + Auth 用户认证

代码语言:javascript
复制
			package cn.netkiller.jersey;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Invocation;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;

public class JerseyClientGetAuth {

	public static void main(String[] args) {

		ClientConfig clientConfig = new ClientConfig();

		HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("neo", "chen");
		clientConfig.register(feature);

		Client client = ClientBuilder.newClient(clientConfig);
		WebTarget webTarget = client.target("http://api.netkiller.cn/v1/withdraw/ping.json").path("");

		Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
		Response response = invocationBuilder.get();

		System.out.println(response.getStatus());
		System.out.println(response.getStatusInfo());

		if (response.getStatus() == 200) {

			String output = response.readEntity(String.class);
			System.out.println(output);

		}

	}
}			
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2016-09-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 Netkiller 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 第 20 章 Jersey - RESTful Web Services in Java.
    • 20.1. Client
      • 20.1.1. Maven 版本
      • 20.1.2. GET 操作
      • 20.1.3. GET + Auth 用户认证
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档