web.xml:
<!-- begin xfire -->
<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下开放Web Service服务 -->
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<!-- end xire -->
xfire-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 引入XFire预配置信息 -->
<import resource="classpath:org/codehaus/xfire/spring/xfire.xml" />
<!-- Web服务实现类,就是要发布成web服务的pojo,标注了@WebService注解 -->
<bean id="userService" class="com.test.xfire.UserServiceImpl" />
<!-- 获得applicationContext中所有bean的JSR181 annotation -->
<!-- 该Bean获取Spring容器中所有标注@WebService的Bean -->
<bean id="webAnnotations" class="org.codehaus.xfire.annotations.jsr181.Jsr181WebAnnotations" />
<!-- 对标注@WebService的Bean进行处理,完成导出工作 -->
<bean id="jsr181HandlerMapping" class="org.codehaus.xfire.spring.remoting.Jsr181HandlerMapping">
<property name="xfire" ref="xfire" />
<property name="webAnnotations" ref="webAnnotations" />
</bean>
</beans>
package com.test.xfire;
public class User
{
private String username;
private int age;
private String hobby;
public String getUsername()
{
return username;
}
public void setUsername(String username)
{
this.username = username;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public String getHobby()
{
return hobby;
}
public void setHobby(String hobby)
{
this.hobby = hobby;
}
public String toString()
{
return "用户:" + username + ",年龄" + age + "时,爱好:" + hobby;
}
}
package com.test.xfire;
import javax.jws.WebService;
@WebService
public interface IUserService
{
public User findUserHobby(User user) throws Exception;
}
package com.test.xfire;
import java.util.HashMap;
import java.util.Map;
import javax.jws.WebService;
/**
* serviceName是这这个服务的名称,默认为接口实现类的名称,endpointInterface是该类实现的接口的类全名<BR>
* 访问:http://localhost:8080/ssh/service/userServiceImpl?wsdl
*
* @author Administrator
*/
@WebService(serviceName = "userServiceImpl", endpointInterface = "com.test.xfire.IUserService")
public class UserServiceImpl implements IUserService
{
private static final Map<String, String> mapUser = new HashMap<String, String>();
static
{
mapUser.put("jg.sun", "篮球");
mapUser.put("lcrystal", "足球");
mapUser.put("s0meb0dy", "游泳");
mapUser.put("猫来猫去", "睡觉");
mapUser.put("小刚", "唱歌");
}
public User findUserHobby(User user) throws Exception
{
if (user == null)
{
return null;
}
String hobby = mapUser.get(user.getUsername());
if (hobby == null)
{
user.setHobby("无");
}
else
{
user.setHobby(hobby);
}
return user;
}
}
客户端调用2种方式:
1、通过WSDL文件生成客户端调用程序,先在目录存放WSDL文件
package com.test.xfire.client;
import org.codehaus.xfire.client.Client;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import com.test.xfire.IUserService;
import com.test.xfire.User;
/**
* 通过WSDL文件生成客户端调用程序
*
* @author Administrator
*/
public class WebServiceClientTestByWsdl
{
IUserService iUserService = null;
public static void main(String[] args) throws Exception
{
WebServiceClientTest test = new WebServiceClientTest();
test.testClient();
}
public void testClient() throws Exception
{
String wsdl = "userServiceImpl.wsdl"; // 对应的WSDL文件
Resource resource = new ClassPathResource(wsdl);
Client client = new Client(resource.getInputStream(), null); // 根据WSDL创建客户实例
User user = new User();
user.setUsername("猫来猫去");
Object[] objArray = new Object[1];
objArray[0] = user;
// 调用特定的Web Service方法
Object[] results = client.invoke("findUserHobby", objArray);
System.out.println("result: " + results[0]);
}
}
2、根据服务地址创建客户端调用程序
webserviceClient.xml
<?xml version="1.0" encoding="GBK"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<bean id ="testWebService" class ="org.codehaus.xfire.spring.remoting.XFireClientFactoryBean">
<property name ="serviceClass">
<value>com.test.xfire.IUserService</value>
</property>
<property name ="wsdlDocumentUrl">
<value>http://localhost:8080/ssh/service/userServiceImpl?wsdl</value>
</property>
</bean>
</beans>
package com.test.xfire.client;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.xfire.IUserService;
import com.test.xfire.User;
/**
* 根据服务地址创建客户端调用程序
*
* @author Administrator
*/
public class WebServiceClientTest
{
IUserService iUserService = null;
public static void main(String[] args)
{
WebServiceClientTest test = new WebServiceClientTest();
test.testClient();
}
public void testClient()
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("webserviceClient.xml");
iUserService = (IUserService) ctx.getBean("testWebService");
try
{
User user = new User();
user.setUsername("猫来猫去");
System.out.println(iUserService.findUserHobby(user));
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
分享到:
相关推荐
使用xfire+spring编写webservice例子,供大家学习与交流,是一个完整的实例,里面有说明文档
一个webservice实例,有自定义对象,数组的传输
xfire+spring+hibernate webservice
xfire集成spring2.5+hibernate3.3,在网上找的东西,都是文档,所以想上传个实例工程,带jar包,说明文档,SQL脚本
XFire中实现WS-Security完整编 Spring+xFire+wss4j配置Helloworld实例 完整说明文档 Myeclipse项目 服务端工程 客户端工程
JQuery1.4.2+Struts2.1.8+JSON0.34+Spring2.5.6+Hibernate3.5+XFire1.2.6整合实例(已上传) 1、JSON0.34使用的是struts2附带的struts2-json-plugin-2.1.8.1.jar 2、db是mysql,名字为test,用户名root,密码空 3、...
webservice xfire1.2.6 spring2.0完整的代码,包含客户端两种方式(1 接口 2 url)
NULL 博文链接:https://jilongliang.iteye.com/blog/1441583
webservice之xfire的实例文档
NULL 博文链接:https://zw7534313.iteye.com/blog/547916
CXF环境配置 - jackymin CXF与Spring的集成 log4j的配置使用 Spring XML配置十二个最佳实践WebService开发实例 (Xfire+Spring+Hibernate)
invalid SOAP request: 当在浏览其中报此错误的时候,实际上是因为WebService针对的是客户端请求,而不是浏览器的访问. 只要建立了测试类,在测试类中访问就可以了. applicationContext.xml 一直是放置在WEB-INF目录...
使用xfire 在myEclipse下开发webservice的实例,包括常用数据格式的映射、xfire与spring的集成配置
3、com.morningstar.utility.WSUtility为调用WebService的客户端代码,在调用这个WebService返回数据时存在一点问题。希望有人能帮我解决下,指点下我,小弟不胜感激。EncryUtility为加密类,有MD5和SHA可选。 4、...
主要描述了s2sh框架下xfire配置webservice的方法,以及实例。绝对是处于本人之手,并且验证过,绝对真实.
里面包含了两个工程,WineService是开发服务,WineServiceClient是客户端测试! 使用xFire框架
XFire Java SOAP框架概述 MyEclipse Web Services是建立在XFire Java ... XFire支持 Web Services standards,Spring 整合, 支持JBI , 支持 JAXB ,XMLBeans,Java 5 和JAX-WS.并支持HTTP, JMS, XMPP, In-memory传输协议
xfire开发webservice接口程序整合spring实例。
十分的好用
java-WebService应用实例中包括所用的jar包,说明文档,还收集的Spring集成XFire开发WebService的应用