首先加入axis2的pom.xml
<!--axis2版本指定--> <axis2.version>1.6.2</axis2.version> <!--axis2 begin--> <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-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> <!--axis2 end-->
创建要发布的接口和实现类
接口:
public interface HelloWorldService{ public String sayHello(String msg); }
实现类:
@Component("helloWorldService") public class HelloWorldServiceImpl implements HelloWorldService{ @Override public String insertRedPaperWapUser(String msg) { return "Hello"+msg; } }
说明:由于要交个spring来管理,此处用@Component组件来注解,
spring配置省略,其他配置,这里只贴出axis2的spring配置:
<!--axis2交给spring来管理--> <bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
配置web.xml:
<servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class> <load-on-startup>15</load-on-startup> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
接着在你的WEB-INF创建个文件夹services/SimpleService/META-INF/services.xml如图:
<?xml version="1.0" encoding="UTF-8"?> <!-- ~ Licensed to the Apache Software Foundation (ASF) under one ~ or more contributor license agreements. See the NOTICE file ~ distributed with this work for additional information ~ regarding copyright ownership. The ASF licenses this file ~ to you under the Apache License, Version 2.0 (the ~ "License"); you may not use this file except in compliance ~ with the License. You may obtain a copy of the License at ~ ~ http://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by applicable law or agreed to in writing, ~ software distributed under the License is distributed on an ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY ~ KIND, either express or implied. See the License for the ~ specific language governing permissions and limitations ~ under the License. --> <!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 --> <service name="HelloWorldService"> <description>axis2与spring集成</description> <!-- 通过ServiceObjectSupplier参数指定SpringServletContextObjectSupplier类来获得Spring的ApplicationContext对象 --> <parameter name="ServiceObjectSupplier"> org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier </parameter> <!-- SpringBeanName固定的不能改 helloWorldService是spring中注册的实现类得id --> <parameter name="SpringBeanName">helloWorldService</parameter> <!-- 在这里最值得注意的是<messageReceivers>元素,该元素用于设置处理WebService方法的处理器。 例如,getGreeting方法有一个返回值,因此,需要使用可处理输入输出的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>
接着,部署tomcat并启动,打开浏览器输入:http://localhost:8080/hnmccClientWap/services/HelloWorldService?wsdl;
客户端调用也很简单:
System.setProperty("javax.net.ssl.trustStore", "../httpsService/target/jetty-ssl.keystore"); System.setProperty("javax.net.ssl.trustStorePassword", "axis2key"); String epr = "http://localhost:8080/hnmccClientWap/services/HelloWorldService"; Options options = new Options(); options.setTo(new EndpointReference(epr)); ServiceClient sender = null; sender = new ServiceClient(); sender.setOptions(options); OMElement ret = sender.sendReceive(creatMsg()); System.out.println(new String(XXTEA.decryptWithBase64(ret.getFirstElement().getText(), "hnmccztkhdXW45o97YYLwApyehdpVrmy".getBytes(), "utf-8"))); public static OMElement creatMsg() { OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace omNs = fac.createOMNamespace("http://impl.service.xwtec.com", "ns1"); OMElement method = fac.createOMElement("sayHello", omNs); OMElement value = fac.createOMElement("msg", omNs); value.setText("World!"); method.addChild(value); return method; }