jersey 示例获取List<Object>(二)
2016-05-26 16:09
447 查看
1. bean 代码
2. 服务端代码
3. 客户端代码
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(); } } }
相关文章推荐
- [论文阅读]R-FCN: Object Detection via Region-based Fully Convolutional Networks
- Object-C之动态类型(isKindOfClass, isMemberOfClass,id)等
- setObject()用法
- (String)\toString()\String.valueOf(Object) java中的区别
- [Android]JSON数据传输出错——JSONObject将对象数据(Map,List)当成字符串处理
- TypeError: 'str' object is not callable
- Objective-C ---XML解析 (梳理整理)
- Objective-C ---JSON 解析 和 KVC
- Objective-C 记录
- Java Object
- READING NOTE: Object Detection from Video Tubelets with Convolutional Neural Networks
- FreeType, FFmpeg, SDL, 图像处理软件, Mac OS X, Objective-C
- iOS开发实用技巧—Objective-C中的各种遍历(迭代)方式
- Objective-C 和 Swift互相操作
- READING NOTE: R-FCN: Object Detection via Region-based Fully Convolutional Networks
- ORA-12838: cannot read/modify an object after modifying it in parallel
- uva,132 Bumpy Objects (凸包,角度)
- JavaScript基础——引用类型(一)Object类型、Array类型
- objective-c学习推荐网站
- ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory