services.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://xfire.codehaus.org/config/1.0">
<service>
<name>服务名(自己定义的名字)</name>
<namespace>名称空间</namespace>
<serviceClass>接口全路径</serviceClass>
<implementationClass>实现类全路径</implementationClass>//注意单词不能拼错,否责出现大问题
</service>
</beans>
访问测试这个服务发布是否成功: http://localhost:8080/项目名/services/*(*代表服务名,)
创建客户端
(1)导出服务器端的接口jar文件.
(2)引入XFire的jar包. Core.jar及HTTP Client包
(3)创建servlet调用服务.
Service servicemodel=new ObjectServiceFactory().create(接口名.class);
XFire xfire=XFireFactory.newInstance().getXFire();
XFireProxyFactory factory=new XFireProxyFactory(xfire);
String url="服务地址";
接口名 i=null;
i=(接口名)factory.create(servicemodel, url);
//调用方法
Web .xml配置
<?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>
<display-name>XFireServlet</display-name>
<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>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
例如:
- 第一步:建一个secn的services.xml文件 META-INF > xfire > services.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://xfire.codehaus.org/config/1.0">
- <!-- 这个是xfirer的配置文件 -->
- <service>
- <!-- 这个是xfire的名字 -->
- <name>HelloWorldService</name>
- <!-- 这个是名字空间 -->
- <namespace>urn:helloworld:service:xfire:itcast:cn</namespace>
- <!-- 这个是接口 -->
- <serviceClass>cn.itcast.xfire.service.HelloWorld</serviceClass>
- <!-- 这个是实现类 -->
- <implementationClass>cn.itcast.xfire.service.HelloWorldService</implementationClass>
- </service>
- </beans>
- 第二步:建一个web.xml文件
- <!DOCTYPE web-app
- PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
- "http://java.sun.com/dtd/web-app_2_3.dtd">
- <web-app>
- <servlet>
- <servlet-name>XFireServlet</servlet-name>
- <display-name>XFire Servlet</display-name>
- <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>
- <!-- 通过初始化参数改变xfire配置文件的位置 ;如果改改了.那么services.xml就要和web.xml在一起-->
- <!--
- <init-param>
- <param-name>config</param-name>
- <param-value>services.xml</param-value>
- </init-param>
- -->
- </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>
- </web-app>
- 测试方法一:
- @Test
- public void testXfire1() throws Exception{
- Service service = new Service();
- Call call = (Call) service.createCall();
- String url = "http://localhost:8080/secn/services/HelloWorldService" ;
- call.setTargetEndpointAddress(new URL(url));
- call.setOperationName("sayHello");
- System.out.println(call.invoke(new Object[]{"tom"}));
- }
- 测试方法二:这个url地址一定要加上?wsdl并在 new Object[]{"tom"})[0]这里要加上[0]
- @Test
- public void testXfire2() throws Exception{
- String url = "http://localhost:8080/secn/services/HelloWorldService?wsdl" ;
- Client c = new Client(new URL(url));
- System.out.println(c.invoke("sayHello", new Object[]{"tom"})[0]);
- }
- 测试方法三:
- @Test
- public void testXfire3() throws Exception{
- String url = "http://localhost:8080/secn/services/HelloWorldService" ;
- ObjectServiceFactory serviceFactory = new ObjectServiceFactory();
- org.codehaus.xfire.service.Service serviceModel = serviceFactory.create(IHelloWorld.class);
- XFireProxyFactory proxyFactory = new XFireProxyFactory();
- IHelloWorld hw = (IHelloWorld)proxyFactory.create(serviceModel,url);
- System.out.println(hw.sayHello("Tome"));
- }
- 第三步:
- <!-- jsr181的配置,是对于用注释方式生成webService -->
- <service>
- <serviceClass>cn.com.secn.xfire.service.jsr181.CustomerService</serviceClass>
- <!-- serviceFactory>jsr181</serviceFactory -->
- <!-- 这里用的是#号引用下面的Bean -->
- <serviceFactory>#jsr181ServiceFactory</serviceFactory>
- </service>
- <bean id="config" class="org.codehaus.xfire.aegis.type.Configuration">
- <property name="defaultExtensibleElements" value="false" />
- <property name="defaultExtensibleAttributes" value="false" />
- <property name="defaultNillable" value="false" />
- <property name="defaultMinOccurs" value="1" />
- </bean>
- <bean name="jsr181ServiceFactory" class="org.codehaus.xfire.annotations.AnnotationServiceFactory">
- <constructor-arg ref="xfire.transportManager" index="0" />
- <constructor-arg ref="config" index="1" type="org.codehaus.xfire.aegis.type.Configuration" />
- </bean>
- CostomerService类:
- //在这里也可以添服务名
- @WebService
- public class CustomerService {
- private List<Customer> customers = new ArrayList<Customer>();
- public CustomerService() {
- }
- //header = true是以头部发送方式
- @WebMethod
- @WebResult(name = "Customers") //返回类型
- public Collection<Customer> getCustomers(
- @WebParam(name = "UserToken", header = true)
- UserToken auth) {
- authorize(auth);
- return customers;
- }
- private void authorize(UserToken auth) {
- System.out.println(auth.getUsername());
- System.out.println(auth.getPassword());
- }
- @WebMethod
- public String addCustomer(@WebParam(name = "UserToken", header = true)
- UserToken auth, @WebParam(name = "customer") Customer customer) {
- authorize(auth);
- customers.add(customer);
- return "tommm";
- }
- }