1、新建一个接口Application.java(src目录下)
package app;
public interface Application{
public String findUser(String userID);
public String getUserAmount();
public String queryUsers();
}
2、实现接口ApplicationImpl.java(src目录下)
package app;
public class ApplicationImpl implements Application {
public String findUser(String userID){
return "Hello ," + userID;
}
public String getUserAmount(){
return "123";
}
public String queryUsers(){
return "a";
}
}
3、新建webservices.xml(src/META-INF/xfire下)
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>Application</name>
<namespace>http://app/Application</namespace>
<serviceClass>app.Application</serviceClass>
<implementationClass>app.ApplicationImpl</implementationClass>
</service>
</beans>
4、新建web.xml(WebRoot/WEB-INF下)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>XFireServlet</servlet-name>
<servlet-class>
org.codehaus.xfire.transport.http.XFireConfigurableServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/servlet/XFireServlet/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>XFireServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
5、导入xfire/lib下的所有包和xfire-all-1.2.1.jar(WebRoot/WEB-INF/lib下)
这样我们利用tomcat运行工程就OK了,访问地址:http://localhost:8080/XFire/services/Application?wsdl
我们也可以建一个测试程序ApplicationTest.java
package app;
import java.net.MalformedURLException;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
public class ApplicationTest {
public static void main(String[] args) {
Service srvcModel = new ObjectServiceFactory()
.create(Application.class);
XFireProxyFactory factory = new XFireProxyFactory(XFireFactory
.newInstance().getXFire());
String helloWorldURL = "http://localhost:8080/XFire/services/Application";
try {
Application srvc1 = (Application) factory.create(srvcModel,helloWorldURL);
System.out.println(srvc1.findUser("123"));
System.out.println(srvc1.getUserAmount());
System.out.println(srvc1.queryUsers());
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
}
运行控制台显示:
Hello ,123
123
a