日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Apache CXF實(shí)戰(zhàn)之四:構(gòu)建RESTful Web Service

這篇文章介紹一下怎么通過CXF來發(fā)布RESTful的Web Service.

創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括堆龍德慶網(wǎng)站建設(shè)、堆龍德慶網(wǎng)站制作、堆龍德慶網(wǎng)頁制作以及堆龍德慶網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,堆龍德慶網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到堆龍德慶省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!

1. 首先是實(shí)體類,注意其中的@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數(shù)據(jù)格式

 
 
 
  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接口實(shí)現(xiàn)類

 
 
 
  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.測試

首先運(yùn)行MyServer類,然后運(yùn)行MyClient類來驗(yàn)證Web Service。


本文標(biāo)題:Apache CXF實(shí)戰(zhàn)之四:構(gòu)建RESTful Web Service
網(wǎng)頁地址:http://www.dlmjj.cn/article/djohoic.html