基于xfire发布web Service简单实例

  1. 创建接口

    1. package com.test.service;
      public interface CaculatorService {
          public int add(int a,int b);
          public int sub(int a,int b);
      }


  2. 创建实现类

    1. package com.test.service.impl;
      import com.test.service.CaculatorService;
      public class CaculatorServiceImpl implements CaculatorService{
          public int add(int a, int b) {
              return a+b;
          }
          public int sub(int a, int b) {
              return a-b;
          }
      }
  3. 发布服务

    1. 将该应用纳入到xfire应用管辖范围内(通过配置xml,来访问xfire封装的servlet,当按照如下mapping来访问工程的时候都会由xfire提供的这个servlet来处理)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>
          <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>
                                                                                                                                                                           
      </web-app>


    2. services配置文件:来指定提供的接口、实现类、服务的名称;

      在src下新增一个META-INF文件夹在下面新建xfire文件夹,在新建services.xml文件如下:

    3. <?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>
                                                                                                                    
      </web-app>

      考入xfire的jar包,部署工程启动tomcat来访问就ok了

      当客户端发送请求的时候,web.xml会根据servlet mapping访问到xfire的servlet,有该servlet来访问services.xml找到需要发布的服务,这样客户端就可以获得wsdl文件了

你可能感兴趣的:(webservice,xfire)