您好,登錄后才能下訂單哦!
Apache 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); } }
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); }
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; } }
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(); } }
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)); } }
6.測試
首先運行MyServer類,然后運行MyClient類來驗證Web Service。
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。