本文简单介绍axis2单独使用,
1、首先下载axis2-1.6.2-bin.zip,此压缩包中包含了sample、代码生成工具等;
2、创建mvn项目,编写pom.xml,加入axis2依赖包
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.qerooy</groupId>
<artifactId>axis2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<build>
<finalName>axis2</finalName>
</build>
</project>
3、编写User及UserService,临时使用静态数据作测试
package com.qerooy.model;
import java.util.Date;
public class User {
private Integer id;
private String username;
private String remark;
private Date createDatetime;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Date getCreateDatetime() {
return createDatetime;
}
public void setCreateDatetime(Date createDatetime) {
this.createDatetime = createDatetime;
}
}
package com.qerooy.webservice;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.qerooy.model.User;
/**
* 用户服务
*/
public class UserService {
public User findById(Integer id){
return map.get(id);
}
public List<User> findAll(){
return list;
}
private static Map<Integer,User> map = new HashMap<Integer,User>();
private static List<User> list = new ArrayList<User>();
static {
for(int i=0;i<10;i++){
User u = new User();
u.setId(i);
u.setCreateDatetime(new Date());
u.setRemark("remark");
u.setUsername("qerooy"+i);
map.put(i,u);
list.add(u);
}
}
}
4、编写services.xml,同时将目录axis2-1.6.2-bin\axis2-1.6.2\conf中的配置文件axis2.xml放入到项目目录webapp\WEB-INF\conf中
<service name="UserService" scope="application" targetNamespace="http://qerooy.com/">
<description>
User Service
</description>
<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>
<schema schemaNamespace="http://qerooy.com/xsd" />
<parameter name="ServiceClass">com.qerooy.webservice.UserService</parameter>
</service>
5、配置项目web.xml,增加axis2 Servlet
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
<display-name></display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
6、将UserService及配置文件打包成UserService.aar,并将其拷贝到WEB-INF/services目录中即可,可使用命令jar进行打包,本例中使用ant进行,build.xml如下
<project name="axis2" basedir="." default="generate.service">
<property name="AXIS2_HOME" value="F:\tool\axis\axis2-1.6.2-bin\axis2-1.6.2"/>
<property name="build.dir" value="target"/>
<path id="axis2.classpath">
<fileset dir="${AXIS2_HOME}/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="generate.service" >
<delete file="${build.dir}/UserService.aar" />
<!--aar them up -->
<jar destfile="${build.dir}/UserService.aar">
<fileset dir="${build.dir}/classes">
<include name="META-INF/services.xml"/>
<include name="com/qerooy/webservice/*"/>
</fileset>
</jar>
<copy file="${build.dir}/UserService.aar"
toDir="${basedir}/src/main/webapp/WEB-INF/services"
overwrite="yes">
</copy>
</target>
</project>
7、部署项目,此时服务端已完成;
8、本文使用代码生成工具生成客户端代码
wsdl2java -uri http://localhost:8080/axis2/services/UserService?wsdl
9、编写测试代码进行测试
package com.qerooy;
import org.junit.Test;
import com.qerooy.local.UserServiceStub;
import com.qerooy.local.UserServiceStub.FindAll;
import com.qerooy.local.UserServiceStub.FindAllResponse;
import com.qerooy.local.UserServiceStub.FindById;
import com.qerooy.local.UserServiceStub.FindByIdResponse;
public class UserServiceLocalTest {
@Test
public void test() throws Exception{
UserServiceStub userService = new UserServiceStub();
FindAll findAll0 = new FindAll();
FindAllResponse response = userService.findAll(findAll0);
UserServiceStub.User[] userArr = response.get_return();
for(UserServiceStub.User user : userArr){
System.out.println(user.getId()+"-"+user.getUsername()+"-"+user.getRemark());
}
System.out.println("---------");
FindById findById2 = new FindById();
findById2.setId(3);
FindByIdResponse userResponse = userService.findById(findById2);
UserServiceStub.User user = userResponse.get_return();
System.out.println(user.getId()+"-"+user.getUsername()+"-"+user.getRemark());
}
}
完毕!
另服务端如需查看报文可添加日志模块下载的zip包中已有代码 axis2-1.6.2-bin\axis2-1.6.2\samples\userguide\src\userguide\loggingmodule,只需添加此模块即可