微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

axis1 创建service服务端 , axis1 客户端

 
axis1 服务端配置

1、首先建立一个项目 axisTest 不需多说  

2、在lib下放入需要的jar包  点击下载 :axis所需的jar包下载

3、然后需要在web.xml里面加入:

<servlet>
    <servlet-name>AxisServlet</servlet-name>
    display-name>Apache-Axis Servletdisplay-name>
    servlet-class>
        org.apache.axis.transport.http.AxisServlet
    servlet-class>
 servlet>
    url-pattern>/servlet/AxisServleturl-pattern>
 url-pattern>*.jwsurl-pattern>/services/*url-pattern>
 servlet-mapping>  


4、创建服务端的代码

   package com.service;
   public class myService {
       public String getusername(String name){/**用spring的时候需要添加你要注入的bean
           baseTransaction = (BaseTransaction) getApplicationContext().getBean("baseTransaction")**/
           return "Hello "+name+",this is an Axis Web Service"
           }
   }   

5、在WEB-INF下创建service-config.wsdd 文件内容如下:

< deployment   xmlns="http://xml.apache.org/axis/wsdd/"  xmlns:java="http://xml.apache.org/axis/wsdd/providers/java" >
handler type="java:org.apache.axis.handlers.http.URLMapper" name="URLMapper"/>   
   service name="myService" provider="java:RPC">
       <!--这里的name是你的service名字 访问的时候要用得着的-->
       parameter "className" value="com.service.myService"/>
<!--这里的value是你所提供的的供外部访问的方法所在的类-->
        "allowedMethods" "getusername"         <!--供外部访问的方法-->
    service>
transport "http">
 requestFlow>
type="URLMapper"</transport>
</deployment>   
6、启动tomcat 访问地址: http://localhost:8080/axisTest/servlet/AxisServlet

axis1 客户端调用

package com.axistest;
import java.net.MalformedURLException;
import java.rmi.remoteexception;
import javax.xml.rpc.ServiceException;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class myServiceTestorByWSDD {
    static void main(String[] argsthrows ServiceException,MalformedURLException, remoteexception {
    /**假如访问的时候有spring注入的情况 则需要 添加
    baseTransaction = (BaseTransaction) getApplicationContext().getBean("baseTransaction"); 来获取spring的注入信息。实在service层添加的。**/
    String endpoint = "http://localhost:8080/axisTest/services/myService";
    Service service = new Service(); // 创建一个Service实例,注意是必须的!
    Call call = (Call) service.createCall(); // 创建Call实例,也是必须的!
    call.setTargetEndpointAddress(new java.net.URL(endpoint));// 为Call设置服务的位置
    call.setoperationName("getusername"); // 注意方法名与JavaBeanWS.java中一样!!
    String res = (String) call.invoke(new Object[] { "邹萍" }); // 返回String,传入参数
    System.out.println(res);     
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐