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 ...
随机推荐
- yum常规操作的基本用法
比如说,我想用nano编辑器的,发现没有安装, 这个时候你就可以用$ yum search nano 查询nano属于哪个安装包下. 发现就有nano这个安装包, 这个时候安装一下这个安装包,$ su ...
- 孤荷凌寒自学python第四十二天python线程控制之Condition对象
孤荷凌寒自学python第四十二天python的线程同步之Condition对象 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天学习了Condition对象,发现它综合了Event对象 ...
- 条件随机场(Conditional random field)
条件随机场真是把我给折磨坏了啊,本以为一本小小的<统计学习方法>攻坚剩下最后一章,心情还是十分愉悦的,打算一口气把它看完,结果真正啃起来真是无比的艰难啊,每一句对我都好像是天书一般,怎么这 ...
- nyoj 题目36 最长公共子序列
最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...
- PHP文件信息获取函数
知识点: basename():获取文件名,传入第二个参数则只显示文件名,不显示后缀 dirname():获取文件路径 pathinfo():将文件信息存入一个数组,通过索引basename,dirn ...
- ES6 Destructuring Assignment All In One
ES6 Destructuring Assignment All In One ES6 & Destructuring Assignment Axios, vue https://develo ...
- [洛谷P3978][TJOI2015]概率论
题目大意:对于一棵随机生成的$n$个结点的有根二叉树,所有不同构的形态等概率出现(这里同构当且仅当两棵二叉树根相同,并且相同节点的左儿子和右儿子都相同),求叶子节点个数的期望是多少? 题解:令$f_n ...
- 一文看懂Kafka消息格式的演变
摘要 对于一个成熟的消息中间件而言,消息格式不仅关系到功能维度的扩展,还牵涉到性能维度的优化.随着Kafka的迅猛发展,其消息格式也在不断的升级改进,从0.8.x版本开始到现在的1.1.x版本,Kaf ...
- joyOI 选课 【树形dp + 背包dp】
题目链接 选课 题解 基础背包树形dp #include<iostream> #include<cstdio> #include<cmath> #include&l ...
- 洛谷 P2597 [ZJOI2012]灾难 解题报告
P2597 [ZJOI2012]灾难 题目描述 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发 ...