axis 服务端配置开发
1、首先建立项目 名称为 axis, 这个就不用多说了吧 ,需要的jar可以在这里下载
http://download.csdn.net/detail/linlinv3/8799875
!
2、建立service服务端 代码如下:
接口类:package cn.fulong.webservice.server;public interface Axis2WebService {public void getName();public String getUserInfo(String paramString);}
实现类:package cn.fulong.webservice.server.impl;import org.springframework.stereotype.Repository;import cn.fulong.webservice.server.Axis2WebService;public class Axis2WebServiceImpl implements Axis2WebService {public void getName() {System.out.println("axis2 invoke getName method success");}public String getUserInfo(String userName) {String str = "username = " + userName;return str;}}
3、配置web.xml文件
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<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>/servlet/AxisServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>*.jws</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
4、配置spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
<bean id="axis2WebService" class="cn.fulong.webservice.server.impl.Axis2WebServiceImpl"></bean>
</beans>
5、在项目W
ebContent/WEB-INF下面建立services/axis2/META-INF/services.xml,
内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<service name="axis2WebService">
<description>simple spring example</description>
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
</parameter>
<parameter name="SpringBeanName">axis2WebService</parameter>
<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>
</serviceGroup>
注意:其中 springName 就是 spring.xml中 配置的bean id 的name,name 是自己将要发布的接口的名字(自己随便起名,访问的时候需要使用)
6、至此,配置完成,启动项目,通过
http://localhost:8080/axis/services/axis2WebService?wsdl 访问 ;
即 ip:端口/项目名称/services/发布时候的name?wsdl
axis 客户端配置开发
客户端配置就比较简单了,加入需要的axis2的jar包,直接贴代码:
package cn.fulong.webservice.client;import javax.xml.namespace.QName;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.rpc.client.RPCServiceClient;public class Axis2Client {public static final String qnAddress = "http://impl.server.webservice.fulong.cn";public static final String wsAddress = "http://localhost:8080/axis/services/axis2WebService";public String getJsonStr(String method, Object... objects) {RPCServiceClient serviceClient;EndpointReference targetEPR = null;try {serviceClient = new RPCServiceClient();Options options = serviceClient.getOptions();targetEPR = new EndpointReference(wsAddress);options.setTo(targetEPR);QName opAddEntry = new QName(qnAddress, method);if(objects == null || objects.length == 0) {objects = new Object[]{};}Object results[] = serviceClient.invokeBlocking(opAddEntry,objects, new Class[] { String.class });if (results == null || results[0] == null) {return "";}return results[0].toString();} catch (Exception e) {e.printStackTrace();return "";}}public static void main(String[] args) throws Exception {Axis2Client client = new Axis2Client();System.out.println(client.getJsonStr("getUserInfo", "nihao"));}}
输入结果:
username = nihao