Step 1: Create a transmitted JavaBean object (userinfo)
[Java]
View plaincopyprint?
- 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 ..
- }
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?
- Package com. ws. Services;
- Import javax. JWS. WebService;
- Import com. ws. model. userinfo;
- @ WebService
- Public interface iuserservices {
- Public userinfo getuserinfo (string username, integer userage );
- }
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?
- Package com. ws. Services. impl;
- Import javax. JWS. WebService;
- Import com. ws. model. userinfo;
- Import com. ws. Services. iuserservices;
- @ WebService
- Public class userservicesimpl implements iuserservices {
- Public userinfo getuserinfo (string username, integer userage ){
- Return new userinfo (username, userage );
- }
- }
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?
- 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 to facilitate testing
- } Catch (interruptedexception e ){
- E. printstacktrace ();
- }
- System. Out. println ("server exit ...");
- System. Exit (0 );
- }
- }
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?
- 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 result of returning the JavaBean object
- Userinfo user = userservices. getuserinfo ("Vicky", 23 );
- System. Out. println ("username:" + User. GetUserName ());
- System. Out. println ("userage:" + User. getuserage ());
- System. Exit (0 );
- }
- }
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.