Apache CXF Three-combat Transfer Java Object __java

Source: Internet
Author: User

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

Apache CXF One of the actual combat Hello World Web Service

Apache CXF Combat II Integrated sping and Web container

The first two articles describe how to build the most basic web Service through CXF, and the exposed interface parameters and return values are strings, and let's look at a slightly more complex example.

1. The first is an ordinary Pojo object, used to represent an entity class

Package com.googlecode.garbagecan.cxfstudy.jaxws;

Import java.util.Date;

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. Create a Web Service interface class

Package com.googlecode.garbagecan.cxfstudy.jaxws;

Import Javax.jws.WebMethod;
Import Javax.jws.WebParam;
Import Javax.jws.WebResult;
Import Javax.jws.WebService;

@WebService Public
interface CustomerService {
    @WebMethod
    @WebResult Customer Findcustomer (@WebParam String ID);
3. Create a Web service interface implementation class

Package com.googlecode.garbagecan.cxfstudy.jaxws;

Import Java.util.Calendar;

public class Customerserviceimpl implements CustomerService {public

    Customer Findcustomer (String id) {
        Customer customer = new Customer ();
        Customer.setid ("Customer_" + ID);
        Customer.setname ("Customer_name");
        Customer.setbirthday (Calendar.getinstance (). GetTime ());
        return customer;
    }
4. The following is the server-side code

Package com.googlecode.garbagecan.cxfstudy.jaxws;

Import Javax.xml.ws.Endpoint;

Import Org.apache.cxf.interceptor.LoggingInInterceptor;
Import Org.apache.cxf.interceptor.LoggingOutInterceptor;
Import Org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class MyServer {
    
    private static final String address = "Http://localhost:9000/ws/jaxws/customerService";
    
    public static void Main (string[] args) throws Exception {
        //HTTP://LOCALHOST:9000/WS/JAXWS/CUSTOMERSERVICE?WSDL
        Jaxwsserverfactorybean Factorybean = new Jaxwsserverfactorybean ();
        Factorybean.getininterceptors (). Add (New Loggingininterceptor ());
        Factorybean.getoutinterceptors (). Add (New Loggingoutinterceptor ());

        Factorybean.setserviceclass (customerserviceimpl.class);
        Factorybean.setaddress (address);
        Factorybean.create ();
    }
5. The following is the client-side code

Package com.googlecode.garbagecan.cxfstudy.jaxws;

Import java.net.SocketTimeoutException;

Import javax.xml.ws.WebServiceException;

Import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean; public class MyClient {public static void main (string[] args) throws Exception {Jaxwsproxyfactorybean factor
        Ybean = new Jaxwsproxyfactorybean ();
        Factorybean.setaddress ("Http://localhost:9000/ws/jaxws/customerService");
        Factorybean.setserviceclass (Customerservice.class);

        Object obj = Factorybean.create ();
        CustomerService customerservice = (customerservice) obj;
            try {Customer customer = Customerservice.findcustomer ("123");
        System.out.println ("Customer:" + customer); catch (Exception e) {if (e instanceof webserviceexception && e.getcause () Insta
            Nceof sockettimeoutexception) {System.err.println ("This is timeout exception.");
               } else { E.printstacktrace ();
 }
        }
    }
}
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.