Apache CXF实战之四:构建RESTful Web Service

开发 后端
前面几篇文章介绍了怎样通过CXF来构建最基本的Web Service,本文将介绍一下怎么通过CXF来发布RESTful的Web Service.

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

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

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

2. RESTful Web Service接口类,可以通过修改@Produces注解来声明暴露接口返回的json还是xml数据格式

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

3. RESTful Web Service接口实现类

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

4. Server端代码

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

5. Client端代码

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

6.测试

首先运行MyServer类,然后运行MyClient类来验证Web Service。

 

原文链接:http://blog.csdn.net/kongxx/article/details/7527115

【系列文章】

  1. Apache CXF实战之五:压缩Web Service数据
  2. Apache CXF实战之四:构建RESTful Web Service
  3. Apache CXF实战之三:传输Java对象
  4. Apache CXF实战之二:集成Sping与Web容器
  5. Apache CXF实战之一:Hello World Web Service
责任编辑:林师授 来源: kongxx的博客
相关推荐

2012-05-03 11:51:59

ApacheCXFJava

2012-05-07 14:15:41

ApacheCXFJava

2012-05-07 14:08:20

ApacheCXFJava

2012-05-03 11:21:58

ApacheCXFJava

2012-05-03 11:30:04

ApacheCXFJava

2012-05-03 11:35:56

ApacheCXFJava

2013-03-12 09:50:45

GoRESTful Web

2009-06-18 09:42:52

SpringXFire构建Web

2012-02-22 22:56:19

开源Apache

2011-03-21 14:41:04

LAMPapacheweb

2016-12-09 09:23:50

android组件Service

2011-12-07 10:56:29

ApacheMakeJava

2012-05-03 10:24:02

ApacheMINAJava

2011-04-28 15:18:59

2012-05-03 10:55:51

ApacheMINAJava

2009-12-23 09:50:45

JAX-RSJerseyRESTful

2014-11-18 11:20:51

webserviceCXFSpring

2023-06-29 07:45:03

2012-02-28 15:39:48

2012-02-15 10:40:37

JavaJava Socket
点赞
收藏

51CTO技术栈公众号