Apache CXF实战之四 构建RESTful Web Service

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

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

[java] view plaincopyprint?

<EMBED id=ZeroClipboardMovie_1 name=ZeroClipboardMovie_1 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer height=14 width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=1&width=29&height=14" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">

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

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

[java] view plaincopyprint?

<EMBED id=ZeroClipboardMovie_2 name=ZeroClipboardMovie_2 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer height=14 width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=2&width=29&height=14" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">

  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接口实现类

[java] view plaincopyprint?

<EMBED id=ZeroClipboardMovie_3 name=ZeroClipboardMovie_3 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer height=14 width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=3&width=29&height=14" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">

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

4. Server端代码

[java] view plaincopyprint?

<EMBED id=ZeroClipboardMovie_4 name=ZeroClipboardMovie_4 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer height=14 width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=4&width=29&height=14" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">

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

5. Client端代码

[java] view plaincopyprint?

<EMBED id=ZeroClipboardMovie_5 name=ZeroClipboardMovie_5 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer height=14 width=29 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=5&width=29&height=14" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">

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

6.测试

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

你可能感兴趣的:(Apache CXF实战之四 构建RESTful Web Service)