目录结构:
Axis2Demo
|--src
|--Axis2Server
|--TestServer.java
|--resources
|--appContext.xml
|--springmvc.xml
|--WebRoot
|--WEB-INF
|--services(固定名)
|--myservices(此名可随便)
|--META-INF(固定名)
|--services.xml(固定名)
|--web.xml
各个文件内容:
服务具体逻辑实现类
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;
}
}
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>
<bean id = "applicationContext" class = "org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder">bean>
beans>
web.xml中通过
<servlet>
<servlet-name>SpringMVCservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:resources/springmvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>SpringMVCservlet-name>
<url-pattern>/rest/*url-pattern>
servlet-mapping>
指定的springmvc配置文件
<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>
将pojo发布为webservice的关键文件,此处配置为集成spring框架的配置
<serviceGroup>
<service name="mySoapService">
<description>
axis2 example
description>
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
parameter>
<parameter name="SpringBeanName">testServerparameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
messageReceivers>
service>
serviceGroup>
web项目入口,配置Context内容与servlet、filter,特别注意axis2配置中拦截的地址不要使用axis2/*
否则会出现异常
<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>BlazeDSdisplay-name>
<description>BlazeDS Applicationdescription>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:resources/appContext.xmlparam-value>
context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
listener-class>
listener>
<servlet>
<servlet-name>SpringMVCservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath*:resources/springmvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>SpringMVCservlet-name>
<url-pattern>/rest/*url-pattern>
servlet-mapping>
<servlet>
<servlet-name>AxisServletservlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServletservlet-class>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>AxisServletservlet-name>
<url-pattern>/services/*url-pattern>
servlet-mapping>
web-app>
因为本项目选用maven作为构建端,故将pom文件中关于axis2的内容也贴出来,若有jar包冲突请自行解决
<axis2.version>1.6.2axis2.version>
<dependency>
<groupId>org.apache.axis2groupId>
<artifactId>axis2artifactId>
<version>${axis2.version}version>
dependency>
<dependency>
<groupId>org.apache.axis2groupId>
<artifactId>axis2-springartifactId>
<version>${axis2.version}version>
dependency>
<dependency>
<groupId>org.apache.axis2groupId>
<artifactId>axis2-adbartifactId>
<version>${axis2.version}version>
dependency>
<dependency>
<groupId>org.apache.axis2groupId>
<artifactId>axis2-kernelartifactId>
<version>${axis2.version}version>
dependency>
<dependency>
<groupId>org.apache.axis2groupId>
<artifactId>axis2-transport-httpartifactId>
<version>${axis2.version}version>
dependency>
<dependency>
<groupId>org.apache.axis2groupId>
<artifactId>axis2-transport-localartifactId>
<version>${axis2.version}version>
dependency>
<dependency>
<groupId>org.apache.axis2groupId>
<artifactId>org.apache.axis2.osgiartifactId>
<version>${axis2.version}version>
dependency>
<dependency>
<groupId>org.apache.axis2groupId>
<artifactId>axis2-jaxwsartifactId>
<version>${axis2.version}version>
dependency>
至此,项目就可以部署到tomcat中观看效果了
打开浏览器,浏览如下地址:http://localhost:8080/TYDGPServer/services/mySoapService?wsdl
大功告成!!
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/TYDGPServer/services/mySoapService");
options.setTo(targetEPR);
// 指定方法的参数值
Object[] opAddEntryArgs = new Object[] {"hello world!"};
// 指定方法返回值的数据类型的Class对象
Class[] classes = new Class[] {String.class};
// 指定要调用的方法及WSDL文件的命名空间,QName指定了要调用的方法名,其中第一个参数为wsdl文件命名空间,填入wsdl:definitions 后跟的内容
QName opAddEntry = new QName("http://Axis2Server.dgp.dist", "greek");
// 调用方法并输出该方法的返回值,invokeBlocking第一个参数为QName指定调用方法名,第二个为参数类型,第三个为返回值类型的Class对象
System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]);
}