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

axis开发webservice

一、获取axis资源

    在axis的官方网站http://ws.apache.org/axis/中获得开源项目,本例中采用axis-bin-1_4.zip

二、部署axis

  1.单独部署

     (1) 解压axis-bin-1_4.zip,将axis-1_4\webapps中的axis拷贝到tomcat\webapps目录中

     (2) 启动tomcat,访问http://localhost:8080/axis/(此地址中的端口8080视tomcat的配置而定),显示如下图

      

  (3) 点击上图Validation链接,验证配置,如果出现NOTFound错误或警告,将相关jar包添加到tomcat\webapps\axis\WEB-INF\lib中,配置正确如下图

  

  (4) webservice列表页面,http://localhost:8080/axis/services,如下图

 

  2.集成到已有项目中

    (1) 将axis-1_4\webapps\axis\WEB-INF\web.xml中的相关配置拷贝到已有项目中的web.xml中

    (2) 将axis-1_4\webapps\axis\WEB-INF\lib中的jar包拷贝到项目中

三、web service开发

    (1) 手动开发

     接口:PrintInterface     

package web.service.print;

public interface PrintInterface {

	public String printStr(String s);

}

     实现类:PrintInterfaceImpl

package web.service.print;

public class PrintInterfaceImpl implements PrintInterface {

	public String printStr(String s) {
		// Todo Auto-generated method stub
		return "打印测试:"+s;
	}

}

    (2) 通过Java2WSDL、WSDL2Java自动生成

       目录结构

      

        a. 编写接口PrintInterface

package web.service.print;

public interface PrintInterface {

	public String printStr(String s);

}

       b. 通过Java2WSDL创建printService.wsdl,命令行进入接口包的根目录src下,执行如下命令,完成后,在src下生成printService.wsdl

java -classpath D:\workspace\workspace3.3.2\ticket\web\WEB-INF\classes -Djava.ext.dirs="D:\workspace\workspace3.3.2\ticket\web\WEB-INF\lib" org.apache.axis.wsdl.Java2WSDL -o printService.wsdl -l http://localhost:8080/axis/services/PrintService -n PrintService web.service.print.PrintInterface

      -classpath  PrintInterface生成的class的根目录

      -Djava.ext.dirs  需要的额外的jar包,此处加载的是axis需要的jar包

     -o  生成wsdl文件

      -l  生成的webservice的url

     -n webservice名称

     web.service.print.PrintInterface  包名+接口名

      c. 通过printService.wsdl自动生成Java代码

          在src目录下,执行如下命令

java -Djava.ext.dirs="D:\workspace\workspace3.3.2\ticket\web\WEB-INF\lib" org.apache.axis.wsdl.WSDL2Java -S true -p web.service.print printService.wsdl


        -S  是否生成deploy.wsdd和undeploy.wsdd

        -p  java代码生成的包

       执行完成后,生成如下java代码

     

        PrintServiceSoapBindingImpl是接口PrintInterface,需将Web Service的业务逻辑添加进去

        PrintServiceSoapBindingStub、PrintInterfaceServiceLocator、PrintInterfaceService实现客户端调用

       文件说明,参见http://www.ibm.com/developerworks/cn/webservices/ws-axisfaq/index.html

      

四、web service发布

  1. jws方式发布

      a. 将PrintInterfaceImpl的代码去掉接口和包路径,如下

public class PrintInterfaceImpl{

	public String printStr(String s) {
		// Todo Auto-generated method stub
		return "打印测试:"+s;
	}

}

      b. 将PrintInterfaceImpl,java改为PrintInterfaceImpl,jws后拷贝到tomcat\webapps\axis\test目录下,发布完成

      c. 验证是否能正常生成wsdl文件

         http://localhost:8080/axis/test/PrintInterfaceImpl.jws?wsdl

 

  2. 以class方式发布

      a. 将上述生成的6个java文件的编译好的class文件拷贝到tomcat\webapps\axis\WEB-INF\classes中,注意包的路径

      b. 将deploy.wsdd、undeploy.wsdd拷贝到tomcat\webapps\axis\WEB-INF下

      c. 在tomcat\webapps\axis\WEB-INF下运行如下命令发布,完成后,在本目录下生成server-config.wsdd

java -Djava.ext.dirs="D:\apache-tomcat-6.0.29\webapps\axis\WEB-INF\lib" org.apache.axis.client.AdminClient -l http://localhost:8080/axis/services/AdminService deploy.wsdd

      d. 验证是否发布成功

          http://localhost:8080/axis/services/PrintService?wsdl

  3. 以jar包方式发布

      a. 将上述自动生成的6个java文件打成jar包:printws.jar

      b. 将printws.jar拷贝到tomat\webapps\axis\WEB-INF\lib目录下

      c. 将deploy.wsdd、undeploy.wsdd拷贝到tomcat\webapps\axis\WEB-INF下

      d. 在tomcat\webapps\axis\WEB-INF下运行如下命令发布,完成后,在本目录下生成server-config.wsdd

java -Djava.ext.dirs="D:\apache-tomcat-6.0.29\webapps\axis\WEB-INF\lib" org.apache.axis.client.AdminClient -l http://localhost:8080/axis/services/AdminService deploy.wsdd

      e. 验证是否发布成功

          http://localhost:8080/axis/services/PrintService?wsdl

五、web service 卸载

       a. 在tomcat\webapps\axis\WEB-INF下运行如下命令卸载

java -Djava.ext.dirs="D:\apache-tomcat-6.0.29\webapps\axis\WEB-INF\lib" org.apache.axis.client.AdminClient -l http://localhost:8080/axis/services/AdminService undeploy.wsdd

       b. 验证

           http://localhost:8080/axis/services

六、客户端调用

        a. 客户端不引入printws.jar调用

public class TestWebService {

	/**
	 * @param args
	 * @throws ServiceException 
	 * @throws MalformedURLException 
	 * @throws remoteexception 
	 */
	public static void main(String[] args) throws ServiceException,MalformedURLException,remoteexception {
		// Todo Auto-generated method stub
		
//		String endpoint = "http://localhost:8080/axis/test/Calculator.jws";
		String endpoint = "http://localhost:8080/axis/services/PrintService";
		Service service = new Service();
		Call call = (Call) service.createCall();
		call.setTargetEndpointAddress(new URL(endpoint));
		call.setoperationName("printStr");
		String s = (String) call.invoke(new Object[]{"123"});
				
		System.out.println(s);

	}

            b. 客户端引入printws.jar调用的两种方式

                PrintInterfaceServiceLocator调用

public class TestPrint {

	/**
	 * @param args
	 * @throws ServiceException 
	 * @throws remoteexception 
	 */
	public static void main(String[] args) throws ServiceException,remoteexception {
		// Todo Auto-generated method stub
		
		PrintInterfaceService ps = new PrintInterfaceServiceLocator();
		PrintInterface ws = ps.getPrintService();
		String s = ws.printStr("456");
		System.out.println(s);
		
	}

} 

              PrintServiceSoapBindingStub调用

public class TestPrint {

	/**
	 * @param args
	 * @throws ServiceException 
	 * @throws remoteexception 
	 * @throws MalformedURLException 
	 */
	public static void main(String[] args) throws ServiceException,remoteexception,MalformedURLException {
		// Todo Auto-generated method stub
		
		URL endPoint = new URL("http://localhost:8080/axis/services/PrintService");
		PrintServiceSoapBindingStub pstub = new PrintServiceSoapBindingStub(endPoint,new Service());
		String s = pstub.printStr("456");
		System.out.println(s);
		
	}

} 


七、简单的wsdd文件

      a. deploy.wsdd

<deployment name="test" xmlns="http://xml.apache.org/axis/wsdd/" 
    xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"
<service name="PrintService" provider="java:RPC">
    <parameter name="className" value="web.service.print.PrintInterfaceImpl" />
    <parameter name="allowedMethods" value="*" />
</service>
</deployment> 

      b. undeploy.wsdd

<undeployment
    xmlns="http://xml.apache.org/axis/wsdd/">

  <!-- Services from TestServiceService WSDL service -->

  <service name="PrintService"/>
</undeployment>


八、参考文章

        http://blog.csdn.net/m_yeah/article/details/4384532       

        http://zhaoshg.iteye.com/blog/746756       

        http://www.ibm.com/developerworks/cn/webservices/ws-axisfaq/index.html

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

相关推荐