Use xfire spring to build Web Services)

Source: Internet
Author: User
Http://www.dreamjava.com.cn/html/Spring/20070929/561.html
Note: Many xfire examples are just a simple example of helloworld. This article provides an example of how to operate a specific class to solve the problem that beginners can only process strings.

1. Overview

In this example, the business logic class is a user's business logic class, which includes four methods: creating a user, obtaining a single user, obtaining a user group, and obtaining a user list, this document describes how to build a WebService using xfire + spring. Therefore, this example does not involve specific database operations, but only provides simulation implementation. The method is described as follows:

1) User Creation Method: createuser (User user)

Users can be created only by passing in the user object as a parameter;

2) method for getting user information through user name: getuser (string loginname)

Obtain a user object based on the user name. This method returns a user object;

3) returns the array method of all users: getuserarray ()

Returns the user array;

4) method to return the list of all users: getuserlist ()

The returned result is a list object, and the Put object is of the user type.

Ii. Data Object User. Java

In the previous example, create the example2 directory under the src/WebService directory and create the user. Java file under the example2 directory. The following describes the content of the Data Object User. java. The Code is as follows:

Package WebService. example2;

Import java. util. date;

/***//**
* User object.
* @ Author <a href = "mailto: xiexingxing1121@126.com"> amigoxie </a>
* @ Version1.0
* Creationdate: 2007-9-28-10:42:44 AM
*/
Publicclass user implements java. Io. serializable {
Privatestaticfinallongserialversionuid = 1l;

/*** // ** Username */
Private string loginname;

/*** // ** User Password */
Private string password;

/***** // ** Mobile phone number */
Private string mobile;

/*** // ** Creation time */
Private date createtime;

Public date getcreatetime (){
Return createtime;
}

Public void setcreatetime (date createtime ){
This. createtime = createtime;
}

Public String getloginname (){
Return loginname;
}

Public void setloginname (string loginname ){
This. loginname = loginname;
}

Public String getmobile (){
Return mobile;
}

Public void setmobile (string mobile ){
This. Mobile = Mobile;

}

Public String GetPassword (){
Return password;
}

Public void setpassword (string password ){
This. Password = password;
}

Public String tostring (){
Stringbuffer sb = new stringbuffer ();
SB. append ("loginname =" + this. loginname );
SB. append ("| Password =" + this. Password );
SB. append ("| mobile =" + this. Mobile );
SB. append ("| createdate =" + this. createtime );
Return sb. tostring ();
}
}

3. User Service. Java and Its Implementation userserviceimpl. Java

Let's take a look at the content of the business logic interface class userservice. Java in this example:

Package WebService. example2;

Import java. util. List;

/***//**
* User business type.
* @ Author <a href = "mailto: xiexingxing1121@126.com"> amigoxie </a>
* @ Version1.0
* Creationdate: 2007-9-28-10:37:09 AM
*/
Publicinterface userservice {
/***//**
* Get the user object.
* @ Paramloginname: Username
* @ Return returns the user object
*/
Public user getuser (string loginname );

/***//**
* Create a user.
* @ Paramuser user object
* @ Return the content returned by the tostring () method of the created object
*/
Public String createuser (User user );

/***//**
* Obtains an array of user information.
* @ Return returns an array of user information.
*/
Public user [] getuserarray ();

/***//**
* Obtain the user information list.
* @ Return returns the user information list.
* @ Author <a href = "mailto: xiexingxing1121@126.com"> amigoxie </a>
* Creationdate: 2007-9-28-12:55:37 pm
*/
Public list <user> getuserlist ();
}

Its implementation class userserviceimpl. Java is as follows:

Package WebService. example2;

Import java. util. arraylist;
Import java. util. date;
Import java. util. List;

/***//**
* User business implementation class.
* @ Author <a href = "mailto: xiexingxing1121@126.com"> amigoxie </a>
* @ Version1.0
* Creationdate: 2007-9-28-10:54:11 AM
*/
Publicclass userserviceimpl implements userservice {
/***//**
* Get the user object.
* @ Paramloginname: Username
* @ Return returns the user object
*/
Public user getuser (string loginname ){
User user = new user ();
User. setloginname (loginname );
User. setpassword ("12345678 ");
User. setmobile ("13812345678 ");
User. setcreatetime (new date ());
Return user;
}

/***//**
* Create a user.
* @ Paramuser user object
* @ Return the content returned by the tostring () method of the created object
*/
Public String createuser (User user ){
User. setcreatetime (new date ());
Return user. tostring ();
}

/***//**
* Obtains an array of user information.
* @ Return returns an array of user information.
*/
Public user [] getuserarray (){
User [] userlist = new user [4];
Userlist [0] = getuser ("Honey fruit ");
Userlist [1] = getuser ("sterning ");
Userlist [2] = getuser (" Sub ");
Userlist [3] = getuser ("Lao Xiao ");
Return userlist;
}

/***//**
* Obtain the user information list.
* @ Return returns the user information list.
* @ Author <a href = "mailto: xiexingxing1121@126.com"> amigoxie </a>
* Creationdate: 2007-9-28-12:55:37 pm
*/
Public list <user> getuserlist (){
List <user> userlist = new arraylist <user> ();
Userlist. Add (getuser ("A honey fruit "));
Userlist. Add (getuser ("sterning "));
Userlist. Add (getuser (" Sub "));
Userlist. Add (getuser ("Old Xiao "));
Return userlist;
}
}

Note: The list returned by the getuserlist () method must use the generic type. Otherwise, the deployment will fail.

So far, our business has been completed, and the rest of the work is to modify the configuration file and write the test class for testing. Let's continue.

Iv. configuration file applicationcontext. xml and xfire-servlet.xml modifications

In this section, we need to modify the configuration file. First, add the definition of the user logic bean to the applicationcontext. xml file. The added code is as follows:

<Bean id = "userbean" class = "WebService. example2.userserviceimpl"/>

Next we also need to modify the xfire-servlet.xml file to expose our web service, the modified file content is as follows:

<Entry key = "/userservice. ws">

<Ref bean = "userservice"/>

</Entry>

Add the following bean definition:

<Bean id = "userservice" parent = "basewebservice">
<! -- Business service Bean -->
<Property name = "servicebean" ref = "userbean"/>
<! -- Narrow Interface Class of Business Service Bean -->
<Property name = "serviceclass" value = "WebService. example2.userservice"/>
</Bean>

Our web service has been developed. After starting this project, you can access: http: // localhost: 8080/webservice_helloworld/userservice. ws through a browser? WSDL. We can see the WSDL information of this example in the browser.

V. Web Service Testing

In this section, we will test the preceding web service. First, add the following content to the client. xml file in the src directory:

<! -- Example of getting user information -->
<Bean id = "testuserwebservice" class = "org. codehaus. xfire. Spring. remoting. xfireclientfactorybean">
<Property name = "serviceclass"> <value> WebService. example2.userservice </value>
</Property>
<Property name = "wsdldocumenturl">
<Value> http: // localhost: 8080/webservice_helloworld/userservice. ws? WSDL </value>
</Property>
</Bean>

Then, create the userserviceclienttest. Java class under the src/test directory and write the test code as follows:

Package test;

Import java. util. List;

Import org. springframework. Context. applicationcontext;
Import org. springframework. Context. Support. classpathxmlapplicationcontext;

Import WebService. example2.user;
Import WebService. example2.userservice;

/***//**
* User business testing.
* @ Author <a href = "mailto: xiexingxing1121@126.com"> amigoxie </a>
* @ Version 1.0
* Creation date: 2007-9-28-12:03:06 pm
*/
Public class userserviceclienttest {
Public static void main (string [] ARGs) throws exception {
Userserviceclienttest test = new userserviceclienttest ();
Test. testclient ();
}

Public void testclient () throws exception {
Applicationcontext CTX = new classpathxmlapplicationcontext (
"Client. xml ");
Userservice = (userservice) CTX. getbean ("testuserwebservice ");

// Test the getuser () method
System. Out. println ("-------------- getuser ()------------");
User user = userservice. getuser ("Honey fruit ");
System. Out. println (User );

// Test the getarray () method.
System. Out. println ("-------------- getuserarray ()------------");
User [] userarray = userservice. getuserarray ();
For (INT I = 0; I <userarray. length; I ++ ){
System. Out. println ("I =" + I + "|" + userarray [I]);
}

// Test the createuser () method
System. Out. println ("-------------- createuser ()------------");
User newuser = new user ();
Newuser. setloginname ("departure ");
Newuser. setpassword ("123 ");
Newuser. setmobile ("13666666666 ");
String result = userservice. createuser (newuser );
System. Out. println (result );

// Test the getuserlist () method.
List <user> userlist = userservice. getuserlist ();
System. Out. println ("-------------- getuserlist ()------------");
For (INT I = 0; I <userlist. Size (); I ++ ){
System. Out. println ("I =" + I + "|" + userlist. Get (I ));
}
}
}

You can run this class to view the running effect.

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.