目录结构:
Axis2Demo
|--src
|--Axis2Server
|--TestServer.java
|--resources
|--appContext.xml
|--springmvc.xml
|--WebRoot
|--WEB-INF
|--services(固定名)
|--myservices(此名可随便)
|--META-INF(固定名)
|--services.xml(固定名)
|--web.xml
各个文件内容:
TestServer.java
服务具体逻辑实现类
import org.springframework.stereotype.Component;
/**
* @author yanzy
* @description
* @date 2018-03-14 13:46
* @created by intelliJ IDEA
*/
@Component(value = "testServer")
public class TestServer {
public String greek(String greekWord){
System.out.print("hello World!");
return greekWord;
}
}
appContext.xml
web.xml中通过contextConfigLocation指定的spring配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="dist.dgp"></context:component-scan>
<!--Axis2配置-->
<bean id = "applicationContext" class = "org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder"></bean>
</beans>
springmvc.xml
web.xml中通过
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:resources/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
指定的springmvc配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">
<context:component-scan base-package="dist.dgp"
use-default-filters="false"/>
<mvc:annotation-driven/>
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/pages/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
services.xml
将pojo发布为webservice的关键文件,此处配置为集成spring框架的配置
<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<!-- 可以指定发布多个service -->
<service name="mySoapService">
<description>
axis2 example
</description>
<!-- 指定接口类地址 -->
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
<!-- bean名字 -->
<parameter name="SpringBeanName">testServer</parameter>
<!-- 服务级消息接收器 -->
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</messageReceivers>
<!--注释部分为未集成spring时的普通配置-->
<!--<parameter name="ServiceClass">
dist.dgp.Axis2Server.TestServer
</parameter>
<!– Operation级消息接收器:可以为不同的操作指定不同的消息接收器–>
<operation name="greek">
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>-->
</service>
</serviceGroup>
web.xml
web项目入口,配置Context内容与servlet、filter,特别注意axis2配置中拦截的地址不要使用axis2/*
否则会出现异常
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>BlazeDS</display-name>
<description>BlazeDS Application</description>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:resources/appContext.xml</param-value>
</context-param>
<listener>
<!-- Spring提供ServletContextListener的一个实现类ContextLoaderListener,该类可以作为Listener 使用,
它会在创建时自动查找WEB-INF下的applicationContext.xml文件,因此,如果只有一个配置文件,并且文件名为applicationContext.xml,
则只需在web.xml文件中增加以下配置片段就可以了。 -->
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--===============================================springMVC配置===============================================-->
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:resources/springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!--===============================================springMVC配置end===============================================-->
<!--===============================================Axis2配置===============================================-->
<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>
<!--===============================================Axis2配置end===============================================-->
</web-app>
因为本项目选用maven作为构建端,故将pom文件中关于axis2的内容也贴出来,若有jar包冲突请自行解决
<axis2.version>1.6.2</axis2.version>
<!-- axis2相关 -->
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-spring</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-kernel</artifactId>
<version>${axis2.version}</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>org.apache.axis2.osgi</artifactId>
<version>${axis2.version}</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-jaxws</artifactId>
<version>${axis2.version}</version>
</dependency>
至此,项目就可以部署到tomcat中观看效果了
打开浏览器,浏览如下地址:http://localhost:8080/TYDGPServer/services/mySoapService?wsdl
可以看到webservice的描述文档内容:
![7786310-6c53eb675974ee2e.png](https://i-blog.csdnimg.cn/blog_migrate/72b7cabd8fb4b71c6236280e9056e382.webp?x-image-process=image/format,png)
webservice信息描述文档
使用soapUI访问我们发出的webservice如下:
![7786310-7dd6de4f2bd3a31e.png](https://i-blog.csdnimg.cn/blog_migrate/9112ff86de64afedaba064b1d6942b93.webp?x-image-process=image/format,png)
soapUI访问webservice
大功告成!!
ps:java调用webservice
public static void main(String[] args) throws Exception
{
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(
"http://localhost:8080/axis2/services/SimpleService");
options.setTo(targetEPR);
// 指定getGreeting方法的参数值
Object[] opAddEntryArgs = new Object[] {"超人"};
// 指定getGreeting方法返回值的数据类型的Class对象
Class[] classes = new Class[] {String.class};
// 指定要调用的getGreeting方法及WSDL文件的命名空间
QName opAddEntry = new QName("http://ws.apache.org/axis2", "getGreeting");
// 调用getGreeting方法并输出该方法的返回值
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
// 下面是调用getPrice方法的代码,这些代码与调用getGreeting方法的代码类似
classes = new Class[] {int.class};
opAddEntry = new QName("http://ws.apache.org/axis2", "getPrice");
System.out.println(serviceClient.invokeBlocking(opAddEntry, new Object[]{}, classes)[0]);
}