javaproject 用axis2发布

来源:互联网 发布:数据分析 兼职 编辑:程序博客网 时间:2024/06/07 06:18
场景:
        一个已经写好的程序;现在需要部署成webservice;并且这个程序是用spring框架的;

解决:
一: 需要下载axis2-1.5.1-war
      下载之后把解压出来的axis2.war放到tomcat目录下的webapps;
       在浏览器中输入:http://localhost:8080/axis2/;
      能看到一个apache的界面说明ok了;
      接下来才是开始解决问题;
      很明显axis2部署需要tomcat;我们的javaproject显然不满足这个条件;ok现在对需要部署的javaproject(暂且叫它old吧)简单改造成一个      web程序;
      过程:新建一个webproject工程我们暂且叫它new吧;
                把old中的src,lib,conf等文件按对应方式考到new中。
                关键:把之前部署到tomcat webapps下面的conf,modules,services复制到web-inf下面;当然里面lib内容也需要复制过去的。
                          现在你需要在web-inf目录下创建一个AxisService;再在AxisService里面创建META-INF目录;
                          然后在META-INF里面创建文件services.xml;
                          结果如图:
      
  二: 接下来便是配置了:
         1:web.xm 
        <!--这个地方是配置哪些请求是经过axis2的-->
        <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>  
       
      <!--如果你的程序包含spring请配置下面:加载spring的配置文件-->
       <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>  
<context-param>  
      <param-name>contextConfigLocation</param-name>  
      <param-value>/conf/ni.xml</param-value>  
</context-param>

      2:spring-content.xml
        需要增加:
      <bean id="applicationContext"
        class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
       否则会提示找不到applicationContext;

     3:services.xml;
      (1)axis2兼容spring的
    <serviceGroup>
    <service name="NIServiceImp">
    <description>axis2与spring集成案例</description>
    <!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->
    <parameter name="ServiceObjectSupplier">
            org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
    </parameter>
    <!-- 
       SpringBeanName固定的不能改
       helloWorld是spring中注册的实现类得id
     -->
    <parameter name="SpringBeanName">NIService</parameter>
    <!-- 
    在这里最值得注意的是<messageReceivers>元素,该元素用于设置处理WebService方法的处理器。
    例如,getGreeting方法有一个返回值,因此,需要使用可处理输入输出的RPCMessageReceiver类,
    而update方法没有返回值,因此,需要使用只能处理输入的RPCInOnlyMessageReceiver类。
     -->
    <messageReceivers>
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
            class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
        <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
            class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
    </messageReceivers>
</service>
 
</serviceGroup>

  (2):未使用spring的

<serviceGroup>

<service name="HelloWorldService">

<description>Hello World Service</description>

<parameter name="ServiceClass" locked="false">example.HelloWorldService</parameter>

<operation name="hello" mep="http://www.w3.org/2004/08/wsdl/in-out">

<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />

</operation>

<operation name="helloNoParam">

<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />

</operation>

</service>

</serviceGroup>

然后启动你的tomcat:http://localhost:8080/NIService/services/NIServiceImp?wsdl

如果显示正常的话说明ok了;

三:rpc客户端(调用webservice)


package com.xc.axis2.client;

import javax.xml.namespace.QName;

import org.apache.axis2.AxisFault;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.rpc.client.RPCServiceClient;

public class Client {

public static void main(String[] args) throws Exception { 

// ObjectFactory factory = new ObjectFactory(); 

// SimpleMethod method=factory.createSimpleMethod(); 

// System.out.println("client"); 

RPCServiceClient serviceClient; 

serviceClient = new RPCServiceClient(); 

Options options = serviceClient.getOptions(); 

// 这一步指定了该办事的供给地址 

EndpointReference targetEPR = new EndpointReference("http://localhost:8080/NIService/services/NIServiceImp?wsdl"); 

// 将option绑定到该办事地址 

options.setTo(targetEPR); 

// 添加具体要调用的办法,这个可以从该办事的wsdl文件中得知 

// 第一个参数是该办事的targetNamespace,第二个为你所要调用 

// 的operation名称 


QName namespace = new QName("http://ni.activate.service.cattsoft.com", "doNIService");//axis2为办事端项目名 simpleMethod为办法名 


// 设置返回值类型 

// Class[] returnTypes = new Class[] {String.class}; 

// 设置调用的参数 

Object[] param = new Object[] {参数值}>"

};//输入参数 

// 调用办事,获得返回值 

Object[] b = serviceClient.invokeBlocking(namespace, param,new Class[]{String.class}); 

System.out.println(b[0]); 

}














原创粉丝点击