您的位置:首页 > 移动开发 > Objective-C

jersey 示例获取List<Object>(二)

2016-05-26 16:09 447 查看
1. bean 代码

package com.base.pf.restful.client;

import java.io.Serializable;

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

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Podcast implements Serializable {

private static final long serialVersionUID = -8039686696076337053L;

@XmlElement(name = "id")
private Long id;

@XmlElement(name = "title")
private String title;

@XmlElement(name = "linkOnPodcastpedia")
private String linkOnPodcastpedia;

@XmlElement(name = "feed")
private String feed;

@XmlElement(name = "description")
private String description;

public Podcast(String title, String linkOnPodcastpedia, String feed,
String description) {
this.title = title;
this.linkOnPodcastpedia = linkOnPodcastpedia;
this.feed = feed;
this.description = description;

}

public Podcast() {
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getLinkOnPodcastpedia() {
return linkOnPodcastpedia;
}

public void setLinkOnPodcastpedia(String linkOnPodcastpedia) {
this.linkOnPodcastpedia = linkOnPodcastpedia;
}

public String getFeed() {
return feed;
}

public void setFeed(String feed) {
this.feed = feed;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

}


2. 服务端代码

package com.base.pf.restful;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.base.pf.restful.client.Podcast;

@Path("/person")
public class PersonResource {

@GET
@Produces(MediaType.TEXT_PLAIN)
public String getPersons() {
return "Hello person.";
}

@GET
@Path("{id}")
@Produces(MediaType.APPLICATION_XML)
public List<Podcast> getPerson(@PathParam("id") Long id) {
Podcast p = new Podcast();
p.setId(id);
p.setDescription("张三");
p.setFeed("馒头");
p.setLinkOnPodcastpedia("http://www.baidu.com");
p.setTitle("正大光明");
List<Podcast> list = new ArrayList<Podcast>();
list.add(p);
return list;
}
}


3. 客户端代码

package com.base.pf.restful.client;

import java.util.List;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.GenericType;
import com.sun.jersey.api.client.WebResource;

public class JerseyClient {

public static void main(String[] args) {
Client c = Client.create();
WebResource resource = c
.resource("http://localhost:8080/security/rest/person/1");
// ClientResponse response = resource.get(ClientResponse.class);
List<Podcast> list = resource.get(
new GenericType<List<Podcast>>() {});
for(Podcast p :list){
System.out.print(p.getId());
System.out.print("; "+p.getTitle());
System.out.print("; "+p.getDescription());
System.out.print("; "+p.getLinkOnPodcastpedia());
System.out.println();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: