Apache CXF Real-combat four build restful Web Service

Source: Internet
Author: User

This article link: http://blog.csdn.net/kongxx/article/details/7527115

Apache CXF One of the actual combat Hello World Web Service

Apache CXF Combat II Integrated sping and Web container

Apache CXF Three-combat Transfer Java objects

This article describes how to publish a restful web Service via CXF.

1. First is the entity class, note the @xmlrootelement annotation

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 Interface class, you can modify the @produces annotation to declare that the exposed interface returns JSON or XML data format

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

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-side code

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 factoryb EAN = 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-side code

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

Run the MyServer class first, then run the MyClient class to validate the Web Service.








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.