web.xml:
Java代码
<!--beginxfire-->
<servlet>
<!--配合Spring容器中XFire一起工作的Servlet-->
<servlet-name>xfireServlet</servlet-name>
<servlet-class>org.codehaus.xfire.spring.XFireSpringServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>xfireServlet</servlet-name>
<!--在这个URI下开放WebService服务-->
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<!--endxire-->
xfire-servlet.xml
Java代码
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"
"/dtd/spring-beans.dtd">
<beans>
<!--引入XFire预配置信息-->
<importresource="classpath:org/codehaus/xfire/spring/xfire.xml"/>
<!--Web服务实现类,就是要发布成web服务的pojo,标注了@WebService注解-->
<beanid="userService"class="com.test.xfire.UserServiceImpl"/>
<!--获得applicationContext中所有bean的JSR181annotation-->
<!--该Bean获取Spring容器中所有标注@WebService的Bean-->
<beanid="webAnnotations"class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations"/>
<!--对标注@WebService的Bean进行处理,完成导出工作-->
<beanid="jsr181HandlerMapping"class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
<propertyname="xfire"ref="xfire"/>
<propertyname="webAnnotations"ref="webAnnotations"/>
</bean>
</beans>
Java代码
packagecom.test.xfire;
publicclassUser
{
privateStringusername;
privateintage;
privateStringhobby;
publicStringgetUsername()
{
returnusername;
}
publicvoidsetUsername(Stringusername)
{
this.username=username;
}
publicintgetAge()
{
returnage;
}
publicvoidsetAge(intage)
{
this.age=age;
}
publicStringgetHobby()
{
returnhobby;
}
publicvoidsetHobby(Stringhobby)
{
this.hobby=hobby;
}
publicStringtoString()
{
return"用户:"+username+",年龄"+age+"时,爱好:"+hobby;
}
}
Java代码
packagecom.test.xfire;
importjavax.jws.WebService;
@WebService
publicinterfaceIUserService
{
publicUserfindUserHobby(Useruser)throwsException;
}
Java代码
packagecom.test.xfire;
importjava.util.HashMap;
importjava.util.Map;
importjavax.jws.WebService;
/**
*serviceName是这这个服务的名称,默认为接口实现类的名称,endpointInterface是该类实现的接口的类全名<BR>
*访问:http://localhost:8080/ssh/service/userServiceImpl?wsdl
*
*@authorAdministrator
*/
@WebService(serviceName="userServiceImpl",endpointInterface="com.test.xfire.IUserService")
publicclassUserServiceImplimplementsIUserService
{
privatestaticfinalMap<String,String>mapUser=newHashMap<String,String>();
static
{
mapUser.put("jg.sun","篮球");
mapUser.put("lcrystal","足球");
mapUser.put("s0meb0dy","游泳");
mapUser.put("猫来猫去","睡觉");
mapUser.put("小刚","唱歌");
}
publicUserfindUserHobby(Useruser)throwsException
{
if(user==null)
{
returnnull;
}
Stringhobby=mapUser.get(user.getUsername());
if(hobby==null)
{
user.setHobby("无");
}
else
{
user.setHobby(hobby);
}
returnuser;
}
}
客户端调用2种方式:
1、通过WSDL文件生成客户端调用程序,先在目录存放WSDL文件
Java代码
packagecom.test.xfire.client;
importorg.codehaus.xfire.client.Client;
importorg.springframework.core.io.ClassPathResource;
importorg.springframework.core.io.Resource;
importcom.test.xfire.IUserService;
importcom.test.xfire.User;
/**
*通过WSDL文件生成客户端调用程序
*
*@authorAdministrator
*/
publicclassWebServiceClientTestByWsdl
{
IUserServiceiUserService=null;
publicstaticvoidmain(String[]args)throwsException
{
WebServiceClientTesttest=newWebServiceClientTest();
test.testClient();
}
publicvoidtestClient()throwsException
{
Stringwsdl="userServiceImpl.wsdl";//对应的WSDL文件
Resourceresource=newClassPathResource(wsdl);
Clientclient=newClient(resource.getInputStream(),null);//根据WSDL创建客户实例
Useruser=newUser();
user.setUsername("猫来猫去");
Object[]objArray=newObject[1];
objArray[0]=user;
//调用特定的WebService方法
Object[]results=client.invoke("findUserHobby",objArray);
System.out.println("result:"+results[0]);
}
}
2、根据服务地址创建客户端调用程序
webserviceClient.xml
Java代码
<?xmlversion="1.0"encoding="GBK"?>
<beansxmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"xmlns:context="/schema/context"
xmlns:tx="/schema/tx"xmlns:aop="/schema/aop"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans-2.5.xsd
/schema/context
/schema/context/spring-context-2.5.xsd
/schema/tx
/schema/tx/spring-tx-2.5.xsd
/schema/aop
/schema/aop/spring-aop-2.5.xsd">
<beanid="testWebService"class="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
<propertyname="serviceClass">
<value>com.test.xfire.IUserService</value>
</property>
<propertyname="wsdlDocumentUrl">
<value>http://localhost:8080/ssh/service/userServiceImpl?wsdl</value>
</property>
</bean>
</beans>
Java代码
packagecom.test.xfire.client;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importcom.test.xfire.IUserService;
importcom.test.xfire.User;
/**
*根据服务地址创建客户端调用程序
*
*@authorAdministrator
*/
publicclassWebServiceClientTest
{
IUserServiceiUserService=null;
publicstaticvoidmain(String[]args)
{
WebServiceClientTesttest=newWebServiceClientTest();
test.testClient();
}
publicvoidtestClient()
{
ApplicationContextctx=newClassPathXmlApplicationContext("webserviceClient.xml");
iUserService=(IUserService)ctx.getBean("testWebService");
try
{
Useruser=newUser();
user.setUsername("猫来猫去");
System.out.println(iUserService.findUserHobby(user));
}
catch(Exceptione)
{
e.printStackTrace();
}
}
}