Apache CXF实战之四 构建RESTful Web Service

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.   
  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.     }  
  34. }  
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.   
  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. }  
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.   
  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.     }  
  22. }  
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.   
  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.     }  
  16. }  
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.   
  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.     }  
  24. }  
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

posted on 2013-11-06 00:36  heartstage  阅读(367)  评论(0编辑  收藏  举报

导航