关闭

Jersey客户端代码

标签: JAX-RSJersey
354人阅读 评论(0) 收藏 举报
分类:

首先创建一个maven工程

在pom.xml中添加依赖Jar包:

<dependency>
	<groupId>javax.ws.rs</groupId>
	<artifactId>javax.ws.rs-api</artifactId>
	<version>2.0.1</version>
</dependency>

<dependency>
	<groupId>org.glassfish.jersey.core</groupId>
	<artifactId>jersey-client</artifactId>
	<version>2.19</version>
</dependency>

<dependency>
	<groupId>org.glassfish.jersey.media</groupId>
	<artifactId>jersey-media-json-jackson</artifactId>
	<version>2.19</version>
</dependency>

实体类如下:

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * 场馆
 * 
 * @author 卢艳亮
 * @version 2015年6月27日 下午3:22:10
 */
@XmlRootElement
@SuppressWarnings("serial")
public class TVenueEntity implements java.io.Serializable {

	private long venueId;
	/** 场馆名称 */
	private String venueName;
	/** 1代表主场馆 */
	private int master;

	@XmlElement(name = "venueId")
	public long getVenueId() {
		return venueId;
	}

	public void setVenueId(long venueId) {
		this.venueId = venueId;
	}

	@XmlElement(name = "venueName")
	public String getVenueName() {
		return venueName;
	}

	public void setVenueName(String venueName) {
		this.venueName = venueName;
	}

	@XmlElement
	public int getMaster() {
		return master;
	}

	public void setMaster(int master) {
		this.master = master;
	}

}

访问服务的代码如下:

import java.util.List;

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

import com.wanhang.ydsj.venue.rest.entity.TPlaceEntity;
import com.wanhang.ydsj.venue.rest.entity.TVenueEntity;

public class App {
	
	public static String path = "http://localhost:8888/services/erp/";
	
	public static void main(String[] args) {
//		getById();
		findByVenue();
	}
	
	/**
	 * 测试返回一个实体对象
	 */
	public static void getById() {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(path + "venue/1132.json");
		Response response = target.request().get();

		try {
			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed with HTTP error code : " + response.getStatus());
			}
			TVenueEntity venue = response.readEntity(TVenueEntity.class);
			System.out.println("The generated id is " + venue.getVenueName());
		} finally {
			response.close();
			client.close(); // 在真正开发中不要每次关闭client,比如HTTP长连接是由client持有的
		}
	}
	
	/**
	 * 测试返回一个实体对象列表
	 */
	public static void findByVenue() {
		Client client = ClientBuilder.newClient();
		WebTarget target = client.target(path + "venue_3/place.json");
		Response response = target.request().get();

		try {
			if (response.getStatus() != 200) {
				throw new RuntimeException("Failed with HTTP error code : " + response.getStatus());
			}
			List<TPlaceEntity> venues = response.readEntity(new GenericType<List<TPlaceEntity>>(){});
			System.out.println("The generated id is " + venues.size());
		} finally {
			response.close();
			client.close();
		}
	}
}



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:48491次
    • 积分:1060
    • 等级:
    • 排名:千里之外
    • 原创:53篇
    • 转载:39篇
    • 译文:2篇
    • 评论:0条