前言
本文将讲述使用eclipse发布axis2服务的环境准备到发布过程。
1、基础准备
eclipse需要两个插件(eclipse_axis2-插件-直接解压到dropins)
http://download.csdn.net/detail/bestcxx/9721775
然后是axis2的war包和bin包,前者我们可以直接看到axis2服务启动的样子,并且把我们的webservice服务直接添加到这个axis2项目中,后面的bin包则直接为eclipse开发
axis2提供了环境支持。
axis2的war和bin下载(当然你也可以从apache的官网下载较新版本)
下载地址
http://download.csdn.net/detail/bestcxx/9721766
你本地需要有可以启动的服务器,我这里是tomcat
2、解压axis2-1.7.4-war.zip和axis2-1.7.4-bin.zip
得到axis2.war和axis2-1.7.4
对于axis2.war的处理
放到tomcat服务器webapps下启动
访问 http://localhost:8080/axis2/

点击Administration ,右面有个username和password,这是axis2提供的demo的管理员登陆页,默认是 admin/axis2
如果是生产环境一定记得修改,位置为axis2\WEB-INF\conf\axis2.xml
点击Services,就可以看到axis2自带的webservice服务了
对于axis2-1.7.4的处理
把axis2-1.7.4复制到某一个位置,比如你的D盘然后需要在3中设置了
3、在eclipse中配置axis2的开发环境
点击preferences
然后如上图设置axis2的bin路径,里面的一些jar包等会为eclipse提供支持
eclipse中axis2的插件
eclipse的两个axis2的插件解压到eclipse/dropins,然后重启eclipse
这样,你的eclipse中就可以看到这两个东西了,一个用于将java方法生成为服务端,一个则用于生成客户端

4、下面将写个简单的例子
这个例子包含两个部分,一是服务的发布,二是服务的访问
服务的发布包含简单方法的发布和复杂方法的发布
服务的访问包含axis2客户端访问和axis2的RPC访问
4.1 方法1-接口和接口的实现
接口类
-
package stu.demo.service;
-
-
public interface Axis2TestImpl {
-
public String WriteStr(String str);
-
}
实现类,也是具体服务的发布类
-
package stu.demo.service;
-
-
public class AxisTestService implements Axis2TestImpl {
-
-
public String WriteStr(String str){
-
System.out.println("您输入的是:"+str);
-
return str;
-
}
-
-
}
4.2 使用axis2的插件AxisTestService输出为webservice服务
注意红色方框圈中的区域,new来自于eclipse左上角的File

选择服务所在项目的classes文件夹

一路点下去

继续点

还点

再点一下,得到

Service name就是webservice的名字,Class name是方法所在类的路径,然后按下Load,下面会显示Method name,点next

那个location是文件输出路径,你自定义的,下面File name是输出文件的名字(我这个文是后面写的,所以名字和下面的配图不一样)
然后在你设置的位置得到下面的文件(axis2test_service.aar)

4.3 把服务发布到axis2.war中
还记得tomcat中的那个项目吗
先停止tomcat服务器
把axis2test_service.aar复制到axis2这个项目的axis2\WEB-INF\services\下
(记得把axis2.war删除)
然后重启tomcat
这样你就已经把你刚写的服务发布到这个axis2中去了
访问http://localhost:8080/axis2/services/listServices

4.4 编写并发布另外两个服务
刚才发布的是一个以接口实现类形式展现的类,返回的是String类型,下面将发布不实现任何接口的类,以及返回为实体类型的方法
发布的方法不再赘述
不实现接口的类发布为webservice服务
-
package stu.demo.noimpl;
-
-
public class Axis2NoImpl {
-
-
public String WriteYourName(String name){
-
System.out.println("你输入的名字为:"+name);
-
return name;
-
}
-
-
}
eclipse产生的对应的aar文件为 axis2noImpl.aar

返回类型为实体类的发布为webservice服务
需要实体类
-
package stu.demo.fz;
-
-
public class ModelUser {
-
-
private String userName;
-
private String userPwd;
-
public String getUserName() {
-
return userName;
-
}
-
public void setUserName(String userName) {
-
this.userName = userName;
-
}
-
public String getUserPwd() {
-
return userPwd;
-
}
-
public void setUserPwd(String userPwd) {
-
this.userPwd = userPwd;
-
}
-
-
-
-
}
然后是包含发布方法的类
-
package stu.demo.fz;
-
-
public class Axis2Fz {
-
public ModelUser WriteModelUser(ModelUser modelUser){
-
System.out.println("你输入的用户名为:"+modelUser.getUserName());
-
System.out.println("你输入的密码为:"+modelUser.getUserPwd());
-
return modelUser;
-
}
-
}
eclipse产生的对应的aar文件为 axis2Fz.aar

4.5 axis2插件生成客户端访问webservice


这里涉及到要为哪一个webservice设置客户端
所以先获取webservice的wsdl地址
方法如下

如下

然后接着说eclipse的下一步

Next

Next

选择输出到向项目,然后直接选择一个项目的路径(到项目名字即可)
然后Finesh
得到客户端

当然,如果你为复杂的返回类型那个生成客户端,则会有一个实体类文件生成

4.5 axis2的客户端调用和RPC调用
这里不赘述了了,直接把客户端的调用方法和PRC的调用方法贴出来
需要注意的是,如果只是PRC调用的话,对于返回参数为某个实体类的话,需手动创建实体类文件
需要强调的是,对于客户端调用,其方法名字是和你调用的服务的类的名字息息相关的,但是大体思路是
实例化Stub stub
实例化入参类型 入参
实例化返回类型 返回
返回=stub.方法(入参)
返回.getreturn()就是返回值
-
package stu.demo.test;
-
-
import java.rmi.RemoteException;
-
-
import javax.xml.namespace.QName;
-
-
import junit.framework.TestCase;
-
-
import org.apache.axis2.AxisFault;
-
import org.apache.axis2.addressing.EndpointReference;
-
import org.apache.axis2.client.Options;
-
import org.apache.axis2.rpc.client.RPCServiceClient;
-
import org.junit.Test;
-
-
import stu.demo.fz.xsd.ModelUser;
-
import stu.demo.noimpl.Axis2NoImplStub;
-
import stu.demo.noimpl.WriteYourName;
-
import stu.demo.noimpl.WriteYourNameResponse;
-
-
public class Axis2ClientNoImpl {
-
/**
-
* 使用axis2客户端调用
-
*/
-
@Test
-
public void testAxis2NoImpl(){
-
try {
-
String name="Jecket";
-
// Axis2NoImplStub Axis2NoImpl =new Axis2NoImplStub();//这个方法访问地址被封装到了客户端代码
-
Axis2NoImplStub Axis2NoImpl =new Axis2NoImplStub("http://localhost:8080/axis2/services/Axis2NoImpl.Axis2NoImplHttpSoap12Endpoint/");//如果生产地址和测试不一致,则需要指定,灵活运用
-
-
WriteYourName writeYourName=new WriteYourName ();
-
writeYourName.setName(name);
-
-
WriteYourNameResponse writeYourNameResponse=new WriteYourNameResponse();
-
-
writeYourNameResponse=Axis2NoImpl.writeYourName(writeYourName);
-
-
TestCase.assertFalse("方法返回的名字和你输入的名字不一致", writeYourNameResponse.get_return()==name);
-
} catch (AxisFault e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
} catch (RemoteException e) {
-
// TODO Auto-generated catch block
-
e.printStackTrace();
-
}
-
}
-
-
/**
-
* 使用axis2的RPC调用
-
*/
-
@Test
-
public void testAxis2NoImpl2(){
-
String result = "";
-
try {
-
-
String serviceUrl = "http://localhost:8080/axis2/services/Axis2NoImpl?wsdl";
-
//使用RPC方式调用WebService
-
RPCServiceClient serviceClient = new RPCServiceClient();
-
Options options = serviceClient.getOptions();
-
//设置2秒超时
-
options.setTimeOutInMilliSeconds(2000L);
-
//指定调用WebService的URL
-
EndpointReference targetEPR = new EndpointReference(serviceUrl);
-
options.setTo(targetEPR);
-
-
//指定接口方法的参数值
-
Object[] opAddEntryArgs = new Object[] {"Jecket"};
-
//指定方法返回值的数据类型的Class对象
-
Class[] classes = new Class[] { String.class };
-
//指定调用的方法及WSDL文件的命名空间 QName("targetNamespace","method Name");
-
QName qName = new QName("http://noimpl.demo.stu","WriteYourName");
-
//调用getVersioin方法并输出该方法的返回值,
-
//返回对象是一个Object的数组,拿数组的第一个值,转换强转即可
-
result = serviceClient.invokeBlocking(qName,opAddEntryArgs, classes)[0].toString();
-
} catch (Exception e) {
-
e.printStackTrace();
-
result = e.getMessage();
-
-
-
}
-
System.out.println("返回值result="+result);
-
}
-
-
/**
-
* 使用axis2的RPC调用
-
* 入参和反参都是复杂类型
-
*/
-
@Test
-
public void testAxis2Fz(){
-
String result = "";
-
try {
-
-
String serviceUrl = "http://localhost:8080/axis2/services/Axis2Fz?wsdl";
-
//使用RPC方式调用WebService
-
RPCServiceClient serviceClient = new RPCServiceClient();
-
Options options = serviceClient.getOptions();
-
//设置2秒超时
-
options.setTimeOutInMilliSeconds(2000L);
-
//指定调用WebService的URL
-
EndpointReference targetEPR = new EndpointReference(serviceUrl);
-
options.setTo(targetEPR);
-
-
//指定接口方法的参数值
-
ModelUser modelUser=new ModelUser();
-
modelUser.setUserName("Jecket");
-
modelUser.setUserPwd("123456");
-
Object[] opAddEntryArgs = new Object[] {modelUser};
-
//指定方法返回值的数据类型的Class对象
-
Class[] classes = new Class[] { ModelUser.class };
-
//指定调用的方法及WSDL文件的命名空间 QName("targetNamespace","method Name");
-
QName qName = new QName("http://fz.demo.stu","WriteModelUser");
-
//调用getVersioin方法并输出该方法的返回值,
-
//返回对象是一个Object的数组,拿数组的第一个值,转换强转即可
-
modelUser = (ModelUser) serviceClient.invokeBlocking(qName,opAddEntryArgs, classes)[0];
-
-
System.out.println("name:"+modelUser.getUserName());
-
System.out.println("password:"+modelUser.getUserPwd());
-
} catch (Exception e) {
-
e.printStackTrace();
-
result = e.getMessage();
-
-
-
}
-
-
}
-
-
-
-
}
4.6 axis2的webservice服务添加到自己的项目中发布结合spring+maven
首先建议阅读官方文档http://axis.apache.org/axis2/java/core/docs/spring.html
然后就是实践了
1、maven的pom.xml中关于axis2的配置
-
<dependency>
-
<groupId>org.apache.axis2</groupId>
-
<artifactId>axis2-transport-http</artifactId>
-
<version>1.7.2</version>
-
</dependency>
-
<dependency>
-
<groupId>org.apache.axis2</groupId>
-
<artifactId>axis2-spring</artifactId>
-
<version>1.7.2</version>
-
</dependency>
-
<dependency>
-
<groupId>org.apache.axis2</groupId>
-
<artifactId>axis2</artifactId>
-
<version>1.6.2</version>
-
</dependency>
-
<dependency>
-
<groupId>org.apache.axis2</groupId>
-
<artifactId>axis2-transport-local</artifactId>
-
<version>1.7.2</version>
-
</dependency>
-
<dependency>
-
<groupId>org.apache.axis2</groupId>
-
<artifactId>axis2-kernel</artifactId>
-
<version>1.6.2</version>
-
</dependency>
2、编写axis2的对外提供服务的类以及方法(我这里不写接口了,熟悉spring的应该知道最好使用接口以及接口的实现)
我的项目名称是 mavenssh,包路径为com.bestcxx.mavenstu.mavenssh.axis2,类名为Axis2Webservice,方法只有一个叫getStrA
-
package com.bestcxx.mavenstu.mavenssh.axis2;
-
-
public class Axis2Webservice {
-
-
public String getStrA(){
-
String str="123";
-
System.out.println("你输入的是:"+str);
-
return str;
-
}
-
-
}
3、spring的applicationContext.xml中将Axis2Webservice注册为bean
-
<!--如果没有ServletContext配置 则需要增加下面这句 -->
-
<!--<bean id="applicationContext"
-
class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" /> -->
-
-
<bean id="springAwareService" class="com.bestcxx.mavenstu.mavenssh.axis2.Axis2Webservice" scope="prototype"/>
4、web.xml编写
-
<listener>
-
<description>Spring ApplicationContext 载入</description>
-
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
-
</listener>
-
<!-- Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring Context
-
Loader -->
-
<context-param>
-
<param-name>contextConfigLocation</param-name>
-
<param-value>classpath:spring/applicationContext.xml</param-value>
-
</context-param>
-
-
<!-- axis2设置 -->
-
<servlet>
-
<servlet-name>AxisServlet</servlet-name>
-
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
-
</servlet>
-
-
<servlet-mapping>
-
<servlet-name>AxisServlet</servlet-name>
-
<url-pattern>/services/*</url-pattern>
-
</servlet-mapping>
5、最后是services.xml的编写,这里需要注意路径
比如这里我的项目名称为mavenssh
就需要把services.xml放置到项目的如下路径中
-webapp
-WEB-INF
-services
-mavenssh
-META-INF
-services.xml

services.xml的内容为
-
<service name="SpringAwareService"><!-- 访问的时候,这个是wsdl服务的名字 -->
-
<description>
-
simple spring example
-
</description>
-
<parameter name="ServiceObjectSupplier">org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier</parameter>
-
<parameter name="SpringBeanName">springAwareService</parameter><!-- 这个是spring中配置的bean名字 -->
-
<parameter name="getStrA"><!-- 这个是对外提供的服务的具体方法名 -->
-
<span style="white-space:pre"> </span> <messageReceiver class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver"/>
-
</parameter>
-
</service>
6、这样之后,服务就已经可以正常启动和访问了
http://localhost:8085/mavenssh/services/SpringAwareService?wsdl
7、但是控制台提示
Please update your axis2.xml file!
只需把我们上面实验的axis2.war中的WEB_INF/conf/下的axis2.xml复制到mavenssh(你的项目)下的WEB_INF目录下即可
里面有个用户名和密码,建议注释掉。
转载请声明出处:http://blog.csdn.net/bestcxx/article/details/53889270