Java借助axis2发布WebService

时间:2022-02-05 02:47:45

Webservice:

  1、Xml:

  2、WSDL:

    Web service描述语言(WSDL)就是这样一个基于XML标准通用标记语言下的一个子集)的语言,用于描述Web service及其函数、参数和返回值。WSDL既是机器可阅读的,又是人可阅读的,这将是一个很大的好处。

  3、soap simple object access protoacl (简单对象访问协议) :

    限定了xml的格式
      soap 在http(因为有请求体,所以必须是post请求)的基础上传输xml数据
      请求和响应的xml 的格式如: <Envelop><body>//....</body></Envelop>
    operation name:服务提供的方法
    静态方法不能发布为外部服务
    运用jkd自带的代码生成访问服务器的客户端代码 E:/wsimort -s . http://test.cm/?wsdl
    我们可以把webservice看做是web服务器上的一个应用,web服务器是webservice的一个容器
    函数的参数在 http://test.cm/?xsd=1
    JAX-WS是指 java api for xml -WebService
    //测试 WebService服务的 explorer
    Web Service Explorer 可以显示返回的xml格式
    targetNamespace 默认为倒置的包名

一、借助axis2发布wenService服务的一种简单实现 

  第一步:首先要下载开发所需要的jar包
    下载:axis2-1.6.2-war.zip:  http://www.apache.org/dist//axis/axis2/java/core/1.6.2/
    下载完后将axis2.war放至tomcat安装目录下的webapps文件夹下,然后启动tomcat后,在webapps目录下会生成axis2文件夹。
    访问http://localhost:8080/axis2/能看到Apache的webservice页面表示axis2运行成功。

  第二步:拷贝axis2必要文件到webproject中  

    将tomcate发布axis2 -> WEB_INF 下的conf、modules和services 拷贝到当前项目(webProject)的WEB-INF下,并且把axis2 -> WEB-INF -> lib 下的jar拷贝到当前项目lib下;

    Java借助axis2发布WebService

  第三步:配置service.xml

    在当前项目services/webservice/META-INF(没有该路径需要新建)下新建services.xml,配置对应的类格式如下:

    
 <?xml version="1.0" encoding="UTF-8"?>

       <serviceGroup>

         <service name="income" targetNamespace="http://tempuri.org/">

           <schema schemaNamespace="http://tempuri.org/"/>

           <description>axis2 webservice接口</description>
          <parameter name="ServiceClass">tempuri.org.IncomeJC</parameter>
          <operation name="incomeAdd">
            <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"               class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
          </operation>
        </service>              <service name="incomeData" targetNamespace="http://tempuri.org/">
        <schema schemaNamespace="http://tempuri.org/"/>
        <description> WebServices提供到账数据查询接口</description>
        <parameter name="ServiceClass"> tempuri.org.IncomeDataJC</parameter>
        <operation name="incomeList">
          <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
            class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
        </operation>
       </service>     </serviceGroup>
    
1、发布多个接口:services.xml修改为
<serviceGroup>
<service name= "xxx" targetNamespace="http://tempuri.org/" >
<schema schemaNamespace ="http://tempuri.org/"/>
<description > WebServices接口1</description >
<parameter name ="ServiceClass">tempuri.org.xxx</parameter >
<operation name ="function">
<messageReceiver mep= "http://www.w3.org/2004/08/wsdl/in-out"
class= "org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation >
</service>
<service name= "yyy" targetNamespace="http://tempuri.org/" >
...
</service>
</serviceGroup>
2、接口方法消息接收形式(具体限定某方法)
1):有接收值没有返回值
<operation name = "function1">
<messageReceiver mep= "http://www.w3.org/2004/08/wsdl/in-only"
class= "org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</operation >
2):既有接收值也有返回值
<operation name = "function2">
<messageReceiver mep= "http://www.w3.org/2004/08/wsdl/in-out"
class= "org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation >

  第三步:创建类

    新建包tempuri.org,在tempuri.org下新建类IncomeJC。
    代码如下:    

    
package tempuri.org;
      public class IncomeJC {
        public String incomeAdd(String xml) {
        String success = "0";
        try {
          ProjectIncomePub incomePub = readProjectIncome(xml);//读取xml数据
          if (null != incomePub) {
            success = new ProjectIncomeAction().incomeByJC(incomePub);//进一步处理
          }
        } catch (Exception e) {
          //e.printStackTrace();
          System.out.println("到账接口异常");
          return success;
        }
        return success;
      }

  第四步:修改web.xml配置

    在WEB-INF目录下修改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">
        <!--Axis2 config start-->
        <servlet>
          <servlet-name>AxisServlet</servlet-name>
          <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class >
          <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet-mapping>
          <servlet-name>AxisServlet</servlet-name>
          <url-pattern>/services/*</url-pattern>
        </servlet-mapping>
        <!--Axis2 end-->
      </web-app>

  第五步:启动tomcat后访问:http://127.0.0.1:8080/webProject/services/income?wsdl能看到服务信息了。 
   到此Axis2的WebService服务已成功发布
 
二、axis2 客户端调用

  新建一个客户端的程序中使用:

 /***调用网络服务axis调用webservice**/
    this.incomeService();
    /***********************************/
    import javax.xml.namespace.QName;
    import org.apache.axis2.addressing.EndpointReference;
    import org.apache.axis2.client.Options;
    import org.apache.axis2.rpc.client.RPCServiceClient;
    private void incomeService() {
      String xmlStr = "xmlData";//xml格式的参数数据拼成的字符串
      String url = "http://127.0.0.1:8080/webProject/services/systemDDLService";
      String method="incomeJC";
      String webObject = null;
      try {
        RPCServiceClient serviceClient = new RPCServiceClient();
        Options options = serviceClient.getOptions();
        EndpointReference targetEPR = new EndpointReference(url);
        options.setTo(targetEPR);
        /** 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值,method指定方法名 */
        QName opAddEntry = new QName("http://org.tempuri",method);
        /** 参数,如果有多个,继续往后面增加即可,不用指定参数的名称*/
        Object[] opAddEntryArgs = new Object[] {xmlStr};
        // 返回参数类型,这个和axis1有点区别
        /**invokeBlocking方法有三个参数, 其中第一个参数的类型是QName对象,表示要调用的方法名;第二个参数表示要调用的WebService方法的参数值,参数类型为Object[]; 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。 */
        /**注意: 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}
          如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,
          该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同
        */
        Class[] classes = new Class[] { String.class };
        webObject = (String)serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
        System.out.println(webObject);
      }catch (Exception e) {
        e.printStackTrace();
        long end = System.currentTimeMillis();
      }
    }