Apache cxf 3 Transfers Java objects

Source: Internet
Author: User

Link: http://blog.csdn.net/kongxx/article/details/7527094

Hello world web service, one of Apache cxf practices

Apache cxf practice 2 integration of sping and web containers

Previous two articlesArticleThis section describes how to use cxf to build the most basic web service, and the exposed interface parameters and return values are strings. Let's take a look at a slightly more complex example.

1. A common pojo object is used to represent an object 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; @ webservicepublic 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 = new customer (); customer. setid ("customer _" + id); customer. setname ("customer_name"); customer. setbirthday (calendar. getinstance (). gettime (); Return customer ;}}

4. The following is the server endCode

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 exc Eption {// 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 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 factorybean = new jaxwsproxyfactorybean (); factorybean. setaddress ("http: // localhost: 9000/WS/jaxws/customerservice"); factorybean. setserviceclass (customerservice. class); object OBJ = factorybean. create (); customerservice = (customerservice) OBJ; try {customer = customerservice. findcustomer ("123"); system. out. println ("Customer:" + customer);} catch (exception e) {If (E instanceof webserviceexception & E. getcause () instanceof sockettimeoutexception) {system. err. println ("this is timeout exception. ");} else {e. printstacktrace ();}}}}

6. Test

First, run the myserver class, and then run the myclient class to verify 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.