webservice axis 集成spring,开发测试demo
web.xml 配置axis访问路径
<servlet>
<servlet-name>AxisServlet</servlet-name>
<display-name>Apache-Axis Servlet</display-name>
<servlet-class>
org.apache.axis.transport.http.AxisServlet
</servlet-class>
</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>
1.首先导入相关jar
axis.jar,axis-ant.jar,axiom-api-1.2.13.jar,axiom-dom-1.2.13.jar,axiom-impl-1.2.13.jar,axis2-adb-1.6.2.jar,axis2-kernel-1.6.2.jar,
axis2-transport-http-1.6.2.jar,axis2-transport-local-1.6.2.jar,commons-codec-1.3.jar,commons-httpclient-3.1.jar,commons-logging-1.1.1.jar
,httpcore-4.0.jar,neethi-3.0.2.jar,wsdl4j-1.6.2.jar,XmlSchema-1.4.7.jar
web-inf 下建立文件server-config.wsdd 接口配置信息
<service name="接口名称" provider="java:RPC">
<parameter name="className"
value="类全称" />
<parameter name="allowedMethods" value="*" />
</service>
2.编写相关接口以及实现类
package com.sxw.webService;
/**
*
* @author Administrator
*
*/
public interface Test{
/**
* 操作xml写入数据库
* @param xml
* @return
* @throws javax.xml.rpc.ServiceException
*/
public java.lang.String hello(String xml)throws javax.xml.rpc.ServiceException;
}
package com.sxw.webService.impl;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.rpc.ServiceException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.remoting.jaxrpc.ServletEndpointSupport;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import com.sxw.webService.Test;
import com.util.Dom4jParseUtil;
public class TestImpl extends ServletEndpointSupport implements Test, Controller, ApplicationContextAware{
public java.lang.String hello(String xml)throws javax.xml.rpc.ServiceException{
return "你好返还给你:"+xml;
}
}
3.启动服务器
我这里访问的地址 http://localhost:7001/testWebService/services/test?wsdl
能正确访问显示xml格式表示访问成功.
新建立一个Source Folder
public class Test{
public static void main(String[] args) throws AxisFault {
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
//服务地址
EndpointReference targetEPR = new EndpointReference("http://localhost:7001/testWebService/services/test");
//建立连接
options.setTo(targetEPR);
//访问方法
QName opAddEntry = new QName("http://service.cm.com", "insertjwzh110ToJq");
//传入参数设置
Object[] opAddEntryArgs = new Object[] { "我来了" };
//返回参数设置
Class[] classes = new Class[] { String.class };
//方法目标方法,返回需要参数
String xml = (String) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs,classes)[0];
//输出
System.out.println(xml);
}
}
测试访问 2
首先建立一个bat文件编辑如下
set Axis_Lib=E:\work\test\WebRoot\WEB-INF\lib
set Java_Cmd=java -Djava.ext.dirs=%Axis_Lib%
set Output_Path=E:\work\test\test
set Package=com.sxw.webservices.webclient
%Java_Cmd% org.apache.axis.wsdl.WSDL2Java -o%Output_Path% -p%Package% http://localhost:7001/testWebService/services/test?wsdl
双击执行自动生成.
public static void main(String[] args) throws Exception {
TestImplService service =new TestImplServiceImplServiceLocator();
String wsdlUrl = "http://localhost:7001/testWebService/services/test?wsdl";
URL url = new URL(wsdlUrl);
TestImplServiceImpl client = service.gettestWebService(url);
String res = client.hello("测试");
System.out.println(res);
}