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
restfull环境搭建-helloword(二)的更多相关文章
- restfull环境搭建-helloword
原文地址:http://blog.csdn.net/u013158799/article/details/39758341 1. REST和RESTful Web Services的简要说明 REST ...
- restfull环境搭建-helloword(三)
原文地址:http://only81.iteye.com/blog/1689537 This section creates a CRUD (Create, Read, Update, Delete) ...
- Selenium终极自动化测试环境搭建(二)Selenium+Eclipse+Python
Selenium终极自动化测试环境搭建(二)Selenium+Eclipse+Python 前面举例了Selenium+Eclipse+Junit+TestNG自动化测试环境的搭建,在前一篇的基础上, ...
- Apache Cordova开发环境搭建(二)VS Code
原文:Apache Cordova开发环境搭建(二)VS Code 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u011127019/articl ...
- Spring MVC 环境搭建(二)
在Spring MVC 环境搭建(一)中我们知道 spring 的配置是通过 urlmapping 映射到控制器,然后通过实现Controller接口的handlerequest方法转向页面. 但这存 ...
- Appium python自动化测试系列之appium环境搭建(二)
2.1 基础环境搭建 当我们学习新的一项技术开始基本都是从环境搭建开始,本书除了第一章节也是的,如果你连最基础的环境都没有那么我们也没必要去说太多,大概介绍一下: 1.因为appium是支持andr ...
- Kafka:ZK+Kafka+Spark Streaming集群环境搭建(二十一)NIFI1.7.1安装
一.nifi基本配置 1. 修改各节点主机名,修改/etc/hosts文件内容. 192.168.0.120 master 192.168.0.121 slave1 192.168.0.122 sla ...
- monkeyrunner之安卓开发环境搭建(二)
在上一篇文章-安卓开发环境搭建中,我们创建并启动了eclipse自带的安卓模拟器,该模拟器不仅启动慢,而且在使用过程中的反应速度也是出奇的差,经常出现卡机现象.为了解决这种现象,因此,我们又寻找到了更 ...
- [原]项目进阶 之 持续构建环境搭建(二)Nexus私服器
上一篇博文项目进阶 之 持续构建环境搭建(一)架构中,我们大致讲解了一下本系列所搭建环境的基本框架,这次开始我们进入真正的环境搭建实战.重点不在于搭建的环境是否成功和完善,而是在搭建过程中充分认识到每 ...
随机推荐
- python基础、字符串和if条件语句,while循环,跳出循环、结束循环
一:Python基础 1.文件后缀名: .py 2.Python2中读中文要在文件头写: -*-coding:utf8-*- 3.input用法 n为变量,代指某一变化的值 n = inpu ...
- January 15th, 2018 Week 03rd Monday
We got things to do. Places to go. People to see. Futures to make. 我们有很多事情要做,有很多地方要去,有很多人要见,有很多美好的未来 ...
- IntelliJ IDEA 项目结构旁边出现 0%classes,0% lines covered
不知道一不小心点到哪里,项目变成如下形式 使用ctrl + Alt + F6弹出如下框,取消勾选-->点击Show Selected就可以去掉了 官网解释
- 泰泽智能电视(Tizen smart TV)问世
6月2日至4日,泰泽开发人员大会(TDC)在美国洛杉矶举行,会上韓国三星公司展出了一台泰泽智能电视(原型机). 智能电视(Smart TV not to be confused ...
- [NOIP2018]保卫王国
嘟嘟嘟 由于一些知道的人所知道的,不知道的人所不知道的原因,我来发NOIP2018day2T3的题解了. (好像我只是个搬运工--) 这题真可以叫做NOIplus了,跟其他几道比较水的题果然不一样,无 ...
- Excel中row函数的使用方法
1.row函数的含义 1 row函数的含义 返回所选择的某一个单元格的行数. END 2.row函数的语法格式 1 row函数的语法格式 =row(reference) 如果省略reference,则 ...
- 转载 c# automapper 使用(一) https://www.cnblogs.com/caoyc/p/6367828.html
一.最简单的用法 有两个类User和UserDto 1 public class User 2 { 3 public int Id { get; set; } 4 public string Name ...
- go标准库的学习-database/sql/driver
参考:https://studygolang.com/pkgdoc 1>导入方式: import "database/sql/driver" driver包定义了应被数据库驱 ...
- 流程控制之for
for循环是 迭代式循环,其强大之处在于循环取值 用法一: l = [1, 2, 3, 4, 5, 5, 6, 5, 4, 3] for x in l: print(x) info = {'} for ...
- Qt+QGIS二次开发:开发环境搭建(超级详细)
原文链接: 1.qgis二次开发环境搭建(超级详细) 2.QGIS开发教程(1)——QGIS开发准备工作 3.QGIS(2.18.15 源码)+Qt(5/5.9.3)+VS2015(X64)编译