restfull环境搭建-helloword(二)
原文地址:http://only81.iteye.com/blog/1689537
本文描述,获取XML或json格式数据
首先,创建一个bean,比如Todo(JAXB自动将bean文件,转换成xml或者json,需要添加@XmlRootElement)
package sample.hello.resources.bean; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Todo { private String summary; private String description; public String getSummary() { return summary; } public void setSummary(String summary) { this.summary = summary; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
创建对应的resource
package sample.hello.resources; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import sample.hello.resources.bean.Todo; @Path("todo") public class TodoResource { @GET @Produces({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON}) public Todo getXml(){ Todo todo = new Todo(); todo.setSummary("This is first todo"); todo.setDescription("This is my first todo"); return todo; } @GET @Produces({MediaType.TEXT_XML}) public Todo getHtml(){ Todo todo = new Todo(); todo.setSummary("This is first todo"); todo.setDescription("This is my first todo"); return todo; } }
对应的web.xml,配置如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Restfull</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>jersey REST Service</servlet-name> <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> <init-param> <param-name>com.sun.jersey.config.property.packages</param-name> <param-value>sample.hello.resources</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey REST Service</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>
到此为止,服务器端程序,创建完毕。
客户端取xml或json数据程序:
package sample.hello.resources.client; import java.net.URI; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.UriBuilder; import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; import com.sun.jersey.api.client.config.ClientConfig; import com.sun.jersey.api.client.config.DefaultClientConfig; public class TodoTest { public static void main(String[] args) { ClientConfig config = new DefaultClientConfig(); Client client = Client.create(config); WebResource service = client.resource(getBaseURI()); System.out.println(service.path("rest").path("todo").accept(MediaType.TEXT_XML).get(String.class)); System.out.println("...."); System.out.println(service.path("rest").path("todo").accept(MediaType.APPLICATION_JSON).get(String.class)); // Get JSON for application System.out.println("...."); System.out.println(service.path("rest").path("todo").accept(MediaType.APPLICATION_XML).get(String.class)); } private static URI getBaseURI(){ return UriBuilder.fromUri("http://localhost:8080/Restfull").build(); } }
运行结果,如下:
对应源码下载链接:http://pan.baidu.com/s/1jHO5eVs 密码:vqbj
分类:
restful
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
2016-07-20 JSP取得绝对路径