Apache CXF实战之四 构建RESTful Web Service
Apache CXF实战之一 Hello World Web Service
这篇文章介绍一下怎么通过CXF来发布RESTful的Web Service.
1. 首先是实体类,注意其中的@XmlRootElement注解
- package com.googlecode.garbagecan.cxfstudy.jaxrs;
- import java.util.Date;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement(name="Customer")
- public class Customer {
- private String id;
- private String name;
- private Date birthday;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- @Override
- public String toString() {
- return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this);
- }
- }
package com.googlecode.garbagecan.cxfstudy.jaxrs; import java.util.Date; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name="Customer")
public class Customer {
private String id;
private String name;
private Date birthday;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this);
}
}
2. RESTful Web Service接口类,可以通过修改@Produces注解来声明暴露接口返回的json还是xml数据格式
- package com.googlecode.garbagecan.cxfstudy.jaxrs;
- import javax.ws.rs.GET;
- import javax.ws.rs.Path;
- import javax.ws.rs.PathParam;
- import javax.ws.rs.Produces;
- import javax.ws.rs.QueryParam;
- @Path(value = "/customer")
- @Produces("*/*")
- //@Produces("application/xml")
- //@Produces("application/json")
- public interface CustomerService {
- @GET
- @Path(value = "/{id}/info")
- Customer findCustomerById(@PathParam("id")String id);
- @GET
- @Path(value = "/search")
- Customer findCustomerByName(@QueryParam("name")String name);
- }
package com.googlecode.garbagecan.cxfstudy.jaxrs; import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; @Path(value = "/customer")
@Produces("*/*")
//@Produces("application/xml")
//@Produces("application/json")
public interface CustomerService {
@GET
@Path(value = "/{id}/info")
Customer findCustomerById(@PathParam("id")String id); @GET
@Path(value = "/search")
Customer findCustomerByName(@QueryParam("name")String name);
}
3. RESTful Web Service接口实现类
- package com.googlecode.garbagecan.cxfstudy.jaxrs;
- import java.util.Calendar;
- public class CustomerServiceImpl implements CustomerService {
- public Customer findCustomerById(String id) {
- Customer customer = new Customer();
- customer.setId(id);
- customer.setName(id);
- customer.setBirthday(Calendar.getInstance().getTime());
- return customer;
- }
- public Customer findCustomerByName(String name) {
- Customer customer = new Customer();
- customer.setId(name);
- customer.setName(name);
- customer.setBirthday(Calendar.getInstance().getTime());
- return customer;
- }
- }
package com.googlecode.garbagecan.cxfstudy.jaxrs;
import java.util.Calendar;
public class CustomerServiceImpl implements CustomerService {
public Customer findCustomerById(String id) {
Customer customer = new Customer();
customer.setId(id);
customer.setName(id);
customer.setBirthday(Calendar.getInstance().getTime());
return customer;
}
public Customer findCustomerByName(String name) {
Customer customer = new Customer();
customer.setId(name);
customer.setName(name);
customer.setBirthday(Calendar.getInstance().getTime());
return customer;
}
}
4. Server端代码
- package com.googlecode.garbagecan.cxfstudy.jaxrs;
- import org.apache.cxf.interceptor.LoggingInInterceptor;
- import org.apache.cxf.interceptor.LoggingOutInterceptor;
- import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
- public class MyServer {
- public static void main(String[] args) throws Exception {
- JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
- factoryBean.getInInterceptors().add(new LoggingInInterceptor());
- factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
- factoryBean.setResourceClasses(CustomerServiceImpl.class);
- factoryBean.setAddress("http://localhost:9000/ws/jaxrs");
- factoryBean.create();
- }
- }
package com.googlecode.garbagecan.cxfstudy.jaxrs; import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxrs.JAXRSServerFactoryBean; public class MyServer {
public static void main(String[] args) throws Exception {
JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
factoryBean.getInInterceptors().add(new LoggingInInterceptor());
factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.setResourceClasses(CustomerServiceImpl.class);
factoryBean.setAddress("http://localhost:9000/ws/jaxrs");
factoryBean.create();
}
}
5. Client端代码
- package com.googlecode.garbagecan.cxfstudy.jaxrs;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.methods.GetMethod;
- public class MyClient {
- public static void main(String[] args) throws Exception {
- go("http://localhost:9000/ws/jaxrs/customer/1/info");
- go("http://localhost:9000/ws/jaxrs/customer/search?name=abc");
- }
- private static void go(String url) throws Exception {
- HttpClient client = new HttpClient();
- GetMethod method = new GetMethod(url);
- int statusCode = client.executeMethod(method);
- if (statusCode != HttpStatus.SC_OK) {
- System.err.println("Method failed: " + method.getStatusLine());
- }
- byte[] responseBody = method.getResponseBody();
- System.out.println(new String(responseBody));
- }
- }
package com.googlecode.garbagecan.cxfstudy.jaxrs; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod; public class MyClient { public static void main(String[] args) throws Exception {
go("http://localhost:9000/ws/jaxrs/customer/1/info");
go("http://localhost:9000/ws/jaxrs/customer/search?name=abc");
} private static void go(String url) throws Exception {
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(url);
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}
byte[] responseBody = method.getResponseBody();
System.out.println(new String(responseBody));
}
}
6.测试
首先运行MyServer类,然后运行MyClient类来验证Web Service。
http://blog.csdn.net/kongxx/article/details/7527115
Apache CXF实战之四 构建RESTful Web Service的更多相关文章
- 使用Apache CXF和Spring集成创建Web Service(zz)
使用Apache CXF和Spring集成创建Web Service 您的评价: 还行 收藏该经验 1.创建HelloWorld 接口类 查看源码 打印? 1 package ...
- Apache CXF实战之二 集成Sping与Web容器
本文链接:http://blog.csdn.net/kongxx/article/details/7525481 Apache CXF实战之一 Hello World Web Service 书接上文 ...
- 【转】基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)
转载:http://www.cnblogs.com/windwithlife/archive/2013/03/03/2942157.html 一,选择一个合适的,Web开发环境: 我选择的是Eclip ...
- Building a RESTful Web Service Using Spring Boot In Eclipse
一.构建restful web service 创建Maven的java web工程,maven的pom文件加入依赖包 创建包hello Greeting.java package hello; pu ...
- Apache CXF实现Web Service(4)——Tomcat容器和Spring实现JAX-RS(RESTful) web service
准备 我们仍然使用 Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 中的代码作为基础,并 ...
- Apache CXF实现Web Service(3)——Tomcat容器和不借助Spring的普通Servlet实现JAX-RS(RESTful) web service
起步 参照这一系列的另外一篇文章: Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 首先 ...
- Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service
实现目标 http://localhost:9000/rs/roomservice 为入口, http://localhost:9000/rs/roomservice/room为房间列表, http: ...
- SpringBoot实战(十)之使用Spring Boot Actuator构建RESTful Web服务
一.导入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...
- Spring Boot 构建一个 RESTful Web Service
1 项目目标: 构建一个 web service,接收get 请求 http://localhost:8080/greeting 响应一个json 结果: {"id":1,&qu ...
随机推荐
- Selenium+Python自动化之如何绕过登录验证码
一.使用Fiddler抓包 1.一般登陆网站成功后,会生成一个已登录状态的cookie,那么只需要直接把这个值拿到,用selenium进行addCookie操作即可. 2.可以先手动登录一次,然后抓取 ...
- Python 3基础教程15-读文件内容
前面两篇关于写文件和更新文件内容,我们最后都是手动去打开检查是否更新了.现在我们这里通过函数读取之前文件内容,打印到屏幕终端. 运行结果:
- python 读取数据库中文内容显示一堆问号
需要在连接数据库时 设置编码格式 def select_db(self,db_name): self.conn = MySQLdb.connect( host = self.ip, port = se ...
- Entity Framework(四)--EF原理和状态管理
一.原理: 如何查看真正执行的SQL是怎样的? DbContext有一个Database属性,Database属性有一个Log属性,是Action委托类型其中的参数就是sql语句,每次EF执行sql语 ...
- linux的screen常用命令使用记录
新建screen screen 或者 screen -S name - name用于指定一个screen的名字,默认系统随机 暂时退出当前screen ctrl + a + d - 同时按住这三个键即 ...
- 系统编程--标准IO
1.流和FILE对象 对于国际字符集,一个字符可以由一个以上的字节来表示.标准I/O文件流可以用来操作单字节和多字节(宽,wide)字符集.一个流的方向(orientation)决定了字符是以单字节还 ...
- Java中Object.equals和String.equals的区别详解
前言 Java中的堆和常量池的区别是什么呢?Object.equals与String.equals的区别呢?下面让我们通过一个小示例让你明白它- 1.基础知识 Java的存储空间:寄存器.栈.堆.静态 ...
- [BZOJ3600] 没有人的算术 [重量平衡树+权值线段树]
题面 传送门 思路 这道题目是陈立杰论文<重量平衡树和后缀平衡树在信息学奥赛中的应用 >中关于重量平衡树维护序列排名算法的一个应用 具体方法为:令根节点保存一个实数区间$[0,1]$ 若当 ...
- Codeforces 932.A Palindromic Supersequence
A. Palindromic Supersequence time limit per test 2 seconds memory limit per test 256 megabytes input ...
- 汕头市队赛 SRM10 T1模拟只会猜题意
模拟只会猜题意 SRM 10 描述 有一本n个单词的词典,求按下列方法能造出的不相同的词汇数目. 1.词典中的单词是一个词. 2.能分为两部分的,其中前一部分是一个词典词或者其非空前缀,后一部分是一 ...