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

开发 后端
前面几篇文章介绍了怎样通过CXF来构建最基本的Web Service,本文将介绍一下怎么通过CXF来发布RESTful的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);  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.

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);  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.

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;  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.

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();  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

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));  
    }  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

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

2019-05-21 14:33:01

点赞
收藏

51CTO技术栈公众号