WebService-Axis2的方式
1、引入相关的依赖:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<axis2.version>1.7.9</axis2.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!--axis2 begin-->
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
<version>${axis2.version}</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-spring</artifactId>
<version>${axis2.version}</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-http</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-transport-local</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-xmlbeans</artifactId>
<version>${axis2.version}</version>
</dependency>
</dependencies>
<build>
<finalName>axis2-demo</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
1.1、spring整合axis2的配置
1.2、把交给spring容器管理的对象让axis2去访问它。
2.2、新建个services.xml的文件:
内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->
<!--访问的终端名称-->
<service name="ServiceServer">
<description>axis2</description>
<!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 -->
<parameter name="ServiceObjectSupplier" locked="false">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
<!--
SpringBeanName固定的不能改
userServiceImpl是spring中注册的实现类得id,@Component注解
-->
<parameter name="SpringBeanName">userService</parameter>
<!--
<messageReceivers>元素,该元素用于设置处理WebService方法的处理器。
例如,hello方法有一个返回值,因此,需要使用可处理输入输出的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>
1.3,新建个web.xml文件,内容如下:
1.4、提供暴漏出的接口以及对应的实现类,代码如下:
package com.ws.axis2.service;
import com.ws.axis2.domain.User;
public interface UserService {
User add(User user);
User get(Integer userId);
}
package com.ws.axis2.service;
import com.ws.axis2.domain.User;
import org.springframework.stereotype.Service;
@Service("userService")
public class UserServiceImpl implements UserService {
@Override
public User add(User user) {
user.setUserName(user.getUserName());
return user;
}
@Override
public User get(Integer userId) {
User user =new User();
user.setUserId(userId);
user.setUserName("魏朝阳");
return user;
}
}
1.5、User的实体类如下:
package com.ws.axis2.domain;
public class User {
private Integer userId;
private String userName;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
1.6、去访问相应的wsdl文件如下:这时的参数类型是User.这里如果用原生的WebSrvice会报错误的。
访问下get方法:下面的结果就能证明,webService就是通过xml来定义格式的。
2、如何去在客户端做调用呢?
①、先生成客户端调用的类,拷贝到项目中的位置,先去apache Axis2的官网去下载axis2的文件
②、使用wsdl2java的批处理文件。命令如下:
wsdl2java -uri http://localhost:8080/services/ServiceServer?wsdl -p client -s -o stub
-url参数指定了wsdl文件的路径,可以是本地路径,也可以是网络路径。-p参数指定了生成的Java类的包名,
-o参数指定了生成的一系列文件保存的根目录。
-s 同步模式代码
-a 异步模式代码
③、将生成的客户端代码类拷贝到客户端测试调用
④、客户端的主函数的代码如下:
package org.example;
import client.ServiceServerStub;
import org.apache.axis2.AxisFault;
import java.rmi.RemoteException;
public class App {
public static void main( String[] args ) {
try {
ServiceServerStub serviceServerStub=new ServiceServerStub();
ServiceServerStub.Get get = new ServiceServerStub.Get();
get.setUserId(1003);
try {
ServiceServerStub.GetResponse response=serviceServerStub.get(get);
ServiceServerStub.User user = response.get_return();
System.out.println(user.getUserName()+","+user.getUserId());
} catch (RemoteException e) {
e.printStackTrace();
}
} catch (AxisFault axisFault) {
axisFault.printStackTrace();
}
}
}
运行的结果如下:
上面的过程就是客户端调用服务端的接口,从而返回相应的结果。