The simple JavaBean object transfer in the cxf framework is explained.

Source: Internet
Author: User

Step 1: Create a transmitted JavaBean object (userinfo)

[Java]
View plaincopyprint?
  1. Package com. ws. model;
  2. Import javax. xml. Bind. annotation. xmlaccesstype;
  3. Import javax. xml. Bind. annotation. xmlaccessortype;
  4. Import javax. xml. Bind. annotation. xmlrootelement;
  5. Import javax. xml. Bind. annotation. xmltype;
  6. @ Xmlrootelement (name = "userinfo ")
  7. @ Xmlaccessortype (xmlaccesstype. Field)
  8. @ Xmltype (proporder = {"username", "userage "})
  9. Public class userinfo {
  10. Private string username;
  11. Private integer userage;
  12. Public userinfo (string name, integer age ){
  13. This. userage = age;
  14. This. Username = Name;
  15. }
  16. Public userinfo (){
  17. }
  18. // Add the geter/seter method ..
  19. }
Package COM. WS. model; import javax. XML. BIND. annotation. xmlaccesstype; import javax. XML. BIND. annotation. xmlaccessortype; import javax. XML. BIND. annotation. xmlrootelement; import javax. XML. BIND. annotation. xmltype; @ xmlrootelement (name = "userinfo") @ xmlaccessortype (xmlaccesstype. field) @ xmltype (proporder = {"username", "userage"}) public class userinfo {private string username; private integer userage; Public userinfo (string name, integer age) {This. userage = age; this. username = Name;} public userinfo () {}// Add the geter/seter method ..}

Annotation: @ xmlrootelement-specify the name of the XML root element (optional)
@ Xmlaccessortype-control attribute or method serialization, four solutions:
Field-The jaxb tool is automatically bound to XML for each non-static and non-transient attribute, unless xmltransient is specified
None-No Processing
Property-bind the property with the set/get method unless xmltransient is specified
Public_member-bind attributes with set/get methods or with common access permissions unless xmltransient is specified

@ Xmltype-map a class or an enumeration type to an XML schema type

Step 2: Create WebServices server interfaces and implementation classes
1. Create a server interface class

[Java]
View plaincopyprint?
  1. Package com. ws. Services;
  2. Import javax. JWS. WebService;
  3. Import com. ws. model. userinfo;
  4. @ WebService
  5. Public interface iuserservices {
  6. Public userinfo getuserinfo (string username, integer userage );
  7. }
package com.ws.services;import javax.jws.WebService;import com.ws.model.UserInfo;@WebServicepublic interface IUserServices {public UserInfo getUserInfo(String userName, Integer userAge);}

2. Create a server interface implementation class

[Java]
View plaincopyprint?
  1. Package com. ws. Services. impl;
  2. Import javax. JWS. WebService;
  3. Import com. ws. model. userinfo;
  4. Import com. ws. Services. iuserservices;
  5. @ WebService
  6. Public class userservicesimpl implements iuserservices {
  7. Public userinfo getuserinfo (string username, integer userage ){
  8. Return new userinfo (username, userage );
  9. }
  10. }
package com.ws.services.impl;import javax.jws.WebService;import com.ws.model.UserInfo;import com.ws.services.IUserServices;@WebServicepublic class UserServicesImpl implements IUserServices {public UserInfo getUserInfo(String userName, Integer userAge) {return new UserInfo(userName,userAge);}}

3. Create a server and publish the service

[Java]
View plaincopyprint?
  1. Package com. test;
  2. Import javax. xml. ws. endpoint;
  3. Import org. Apache. cxf. jaxws. jaxwsserverfactorybean;
  4. Import com. ws. Services. impl. userservicesimpl;
  5. Public class servertest {
  6. Public servertest (){
  7. // Publish the User Service Interface
  8. Endpoint. Publish ("http: // localhost: 8090/userinfoservices", new userservicesimpl ());
  9. }
  10. Public static void main (string [] ARGs ){
  11. // Start the service
  12. New servertest ();
  13. System. Out. println ("server ready ...");
  14. Try {
  15. Thread. Sleep (1000*300); // sleep for five minutes to facilitate testing
  16. } Catch (interruptedexception e ){
  17. E. printstacktrace ();
  18. }
  19. System. Out. println ("server exit ...");
  20. System. Exit (0 );
  21. }
  22. }
Package COM. test; import javax. XML. WS. endpoint; import Org. apache. cxf. jaxws. jaxwsserverfactorybean; import COM. WS. services. impl. userservicesimpl; public class servertest {public servertest () {// publish the user service interface endpoint. publish ("http: /localhost: 8090/userinfoservices", new userservicesimpl ();} public static void main (string [] ARGs) {// start the service new servertest (); system. out. println ("Server Ready... "); try {thread. sleep (1000*300); // sleep for five minutes for test} catch (interruptedexception e) {e. printstacktrace ();} system. out. println ("server exit... "); system. exit (0 );}}

Step 3: Create and test the WebServices client (here we will only illustrate the test method in the client project)
1. Copy the server's JavaBean and services interface classes to the client project, and the directories must be consistent.
2. Create a test class in the client project

[Java]
View plaincopyprint?
  1. Package com. ws. client;
  2. Import org. Apache. cxf. jaxws. jaxwsproxyfactorybean;
  3. Import com. ws. model. userinfo;
  4. Import com. ws. server. iuserservices;
  5. Public class usertest {
  6. Public static void main (string [] ARGs ){
  7. // Create a WebService client proxy Factory
  8. Jaxwsproxyfactorybean factory = new jaxwsproxyfactorybean ();
  9. // Register the WebService Interface
  10. Factory. setserviceclass (iuserservices. Class );
  11. // Set the WebService address
  12. Factory. setaddress ("http: // localhost: 8090/userinfoservices ");
  13. Iuserservices userservices = (iuserservices) Factory. Create ();
  14. System. Out. println ("INVOKE userinfo WebService ...");
  15. // Test the result of returning the JavaBean object
  16. Userinfo user = userservices. getuserinfo ("Vicky", 23 );
  17. System. Out. println ("username:" + User. GetUserName ());
  18. System. Out. println ("userage:" + User. getuserage ());
  19. System. Exit (0 );
  20. }
  21. }
Package COM. WS. client; import Org. apache. cxf. jaxws. jaxwsproxyfactorybean; import COM. WS. model. userinfo; import COM. WS. server. iuserservices; public class usertest {public static void main (string [] ARGs) {// create a WebService client proxy factory jaxwsproxyfactorybean factory = new jaxwsproxyfactorybean (); // register the WebService Interface factory. setserviceclass (iuserservices. class); // set the WebService address factory. setaddress ("http: // localhost: 8090/userinfoservices"); iuserservices userservices = (iuserservices) factory. create (); system. out. println ("INVOKE userinfo WebService... "); // test the returned userinfo user = userservices of the JavaBean object. getuserinfo ("Vicky", 23); system. out. println ("username:" + User. getUserName (); system. out. println ("userage:" + User. getuserage (); system. exit (0 );}}

Step 4: run the WebServices service. In IE, enter http: // localhost: 8090/userinfoservices? To verify whether the service is successfully released.
Step 4: run the client for verification.

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.