Apache CXF实战之一 Hello World Web Service

Apache CXF实战之二 集成Sping与Web容器

Apache CXF实战之三 传输Java对象

这篇文章介绍一下怎么通过CXF来发布RESTful的Web Service.

1. 首先是实体类,注意其中的@XmlRootElement注解

  1. package com.googlecode.garbagecan.cxfstudy.jaxrs;
  2. import java.util.Date;
  3. import javax.xml.bind.annotation.XmlRootElement;
  4. @XmlRootElement(name="Customer")
  5. public class Customer {
  6. private String id;
  7. private String name;
  8. private Date birthday;
  9. public String getId() {
  10. return id;
  11. }
  12. public void setId(String id) {
  13. this.id = id;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public Date getBirthday() {
  22. return birthday;
  23. }
  24. public void setBirthday(Date birthday) {
  25. this.birthday = birthday;
  26. }
  27. @Override
  28. public String toString() {
  29. return org.apache.commons.lang.builder.ToStringBuilder.reflectionToString(this);
  30. }
  31. }
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数据格式

  1. package com.googlecode.garbagecan.cxfstudy.jaxrs;
  2. import javax.ws.rs.GET;
  3. import javax.ws.rs.Path;
  4. import javax.ws.rs.PathParam;
  5. import javax.ws.rs.Produces;
  6. import javax.ws.rs.QueryParam;
  7. @Path(value = "/customer")
  8. @Produces("*/*")
  9. //@Produces("application/xml")
  10. //@Produces("application/json")
  11. public interface CustomerService {
  12. @GET
  13. @Path(value = "/{id}/info")
  14. Customer findCustomerById(@PathParam("id")String id);
  15. @GET
  16. @Path(value = "/search")
  17. Customer findCustomerByName(@QueryParam("name")String name);
  18. }
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接口实现类

  1. package com.googlecode.garbagecan.cxfstudy.jaxrs;
  2. import java.util.Calendar;
  3. public class CustomerServiceImpl implements CustomerService {
  4. public Customer findCustomerById(String id) {
  5. Customer customer = new Customer();
  6. customer.setId(id);
  7. customer.setName(id);
  8. customer.setBirthday(Calendar.getInstance().getTime());
  9. return customer;
  10. }
  11. public Customer findCustomerByName(String name) {
  12. Customer customer = new Customer();
  13. customer.setId(name);
  14. customer.setName(name);
  15. customer.setBirthday(Calendar.getInstance().getTime());
  16. return customer;
  17. }
  18. }
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端代码

  1. package com.googlecode.garbagecan.cxfstudy.jaxrs;
  2. import org.apache.cxf.interceptor.LoggingInInterceptor;
  3. import org.apache.cxf.interceptor.LoggingOutInterceptor;
  4. import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
  5. public class MyServer {
  6. public static void main(String[] args) throws Exception {
  7. JAXRSServerFactoryBean factoryBean = new JAXRSServerFactoryBean();
  8. factoryBean.getInInterceptors().add(new LoggingInInterceptor());
  9. factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
  10. factoryBean.setResourceClasses(CustomerServiceImpl.class);
  11. factoryBean.setAddress("http://localhost:9000/ws/jaxrs");
  12. factoryBean.create();
  13. }
  14. }
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端代码

  1. package com.googlecode.garbagecan.cxfstudy.jaxrs;
  2. import org.apache.commons.httpclient.HttpClient;
  3. import org.apache.commons.httpclient.HttpStatus;
  4. import org.apache.commons.httpclient.methods.GetMethod;
  5. public class MyClient {
  6. public static void main(String[] args) throws Exception {
  7. go("http://localhost:9000/ws/jaxrs/customer/1/info");
  8. go("http://localhost:9000/ws/jaxrs/customer/search?name=abc");
  9. }
  10. private static void go(String url) throws Exception {
  11. HttpClient client = new HttpClient();
  12. GetMethod method = new GetMethod(url);
  13. int statusCode = client.executeMethod(method);
  14. if (statusCode != HttpStatus.SC_OK) {
  15. System.err.println("Method failed: " + method.getStatusLine());
  16. }
  17. byte[] responseBody = method.getResponseBody();
  18. System.out.println(new String(responseBody));
  19. }
  20. }
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的更多相关文章

  1. 使用Apache CXF和Spring集成创建Web Service(zz)

    使用Apache CXF和Spring集成创建Web Service 您的评价:       还行  收藏该经验       1.创建HelloWorld 接口类 查看源码 打印? 1 package ...

  2. Apache CXF实战之二 集成Sping与Web容器

    本文链接:http://blog.csdn.net/kongxx/article/details/7525481 Apache CXF实战之一 Hello World Web Service 书接上文 ...

  3. 【转】基于CXF Java 搭建Web Service (Restful Web Service与基于SOAP的Web Service混合方案)

    转载:http://www.cnblogs.com/windwithlife/archive/2013/03/03/2942157.html 一,选择一个合适的,Web开发环境: 我选择的是Eclip ...

  4. Building a RESTful Web Service Using Spring Boot In Eclipse

    一.构建restful web service 创建Maven的java web工程,maven的pom文件加入依赖包 创建包hello Greeting.java package hello; pu ...

  5. Apache CXF实现Web Service(4)——Tomcat容器和Spring实现JAX-RS(RESTful) web service

    准备 我们仍然使用 Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service 中的代码作为基础,并 ...

  6. 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 首先 ...

  7. Apache CXF实现Web Service(2)——不借助重量级Web容器和Spring实现一个纯的JAX-RS(RESTful) web service

    实现目标 http://localhost:9000/rs/roomservice 为入口, http://localhost:9000/rs/roomservice/room为房间列表, http: ...

  8. SpringBoot实战(十)之使用Spring Boot Actuator构建RESTful Web服务

    一.导入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...

  9. Spring Boot 构建一个 RESTful Web Service

    1  项目目标: 构建一个 web service,接收get 请求 http://localhost:8080/greeting 响应一个json 结果: {"id":1,&qu ...

随机推荐

  1. Spring整合EhCache详解

    一.EhCache介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开 源Java分布 ...

  2. HTML DOM简易学习笔记

    文字版:https://github.com/songzhenhua/github/blob/master/HTML DOM简易学习笔记.txt 学习地址:http://www.w3school.co ...

  3. Pytest框架介绍

    Pytest框架介绍.安装 pytest是python测试框架,与python自带的unittest测试框架类似,但是比unittest框架使用起来更简洁,功能更强大 pytest特征 1:断言提示信 ...

  4. python import模块熟悉方法

    python import导入的可用模块很多,新增速度较快,无法一次性全部掌握. 掌握熟悉一种模块的方法是非常有价值的技能. 探究模块可以从python解释器入手,具体流程记录如下: 以os模块为例: ...

  5. [muku][1 初始restful api] chorme安装jsonview 插件

    https://github.com/gildas-lormeau/JSONView-for-Chrome https://www.cnblogs.com/androidstudy/p

  6. HDU 4101 Ali and Baba (思路好题)

    与其说这是个博弈,倒不如说是个搜索.这题思路不错,感觉很难把情况考虑周全. 在地图外围填充一圈0,两次BFS,第一次从-1点出发,把从-1到达的0点以及包围0的那一圈石头标记出来.如下图: 1 1 1 ...

  7. qemu中的网络设置

    https://www.cnblogs.com/hukey/p/6436211.html 这个链接里教你怎么操作kvm的各种网络模式,实际操作成

  8. 【bzoj2500】幸福的道路 树形dp+倍增RMQ+二分

    原文地址:http://www.cnblogs.com/GXZlegend/p/6825389.html 题目描述 小T与小L终于决定走在一起,他们不想浪费在一起的每一分每一秒,所以他们决定每天早上一 ...

  9. 利用VS2013 XSLT对 XML进行转换

    1.打开VS2013 2.文件-->新建-->文件-->XML文件 3.文件-->新建-->文件-->XSLT文件 4.CTRL+SHIFT+S 保存2个文件位置 ...

  10. H5单文件压缩插件

    单文件压缩上传 <input type="file" id="file"> 构造函数 function UpFileImg(options){ va ...