Apache CXF practice 4: Build a RESTful Web Service

Source: Internet
Author: User

This article introduces how to use CXF to publish RESTful Web Services.

1. The first is the object class. Pay attention to the @ XmlRootElement annotation in the object class.

 
 
  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 interface class. You can modify the @ Produces annotation to declare whether the json or xml data format returned by the interface is exposed.

 
 
  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 Interface implementation class

 
 
  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 code

 
 
  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 code

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

First, run the MyServer class, and then run the MyClient class to verify the Web Service.

Series of articles]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.