项目目录结构如下:
用cxf方式实现webservice接口的具体步骤:
1.加jar包
cxf实现ws接口所需部分jar包
axis调用ws接口所需jar包
2.修改web.xml文件
添加下面的代码,根据页面url,匹配是否是ws接口
<servlet> <servlet-name>cxf</servlet-name> <display-name>cxf</display-name> <description>Apache CXF Endpoint</description> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>cxf</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
3.修改applicationContext-cxf.xml
修改后的文件为:
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:jaxrs="http://cxf.apache.org/jaxrs" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml"/> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> <jaxws:endpoint id="TestService" address="/TestService"> <jaxws:implementor> <bean class="com.test.ws.server.TestServiceImpl"> </bean> </jaxws:implementor> </jaxws:endpoint> </beans>
4.ws接口(cxf实现)
TestService.java
package com.test.ws.server; import javax.jws.WebService; @WebService(name = "TestService") public interface TestService{ public String testFunc(String para1, String para2, String para3); }
TestServiceImpl.java
package com.test.ws.server; import javax.jws.WebService; @WebService(endpointInterface = "com.test.ws.server.TestService", serviceName = "TestService") public class TestServiceImpl implements TestService { public TestServiceImpl() { super(); } @Override public String testFunc(String para1, String para2, String para3) { System.out.println("para1:"+para1+",para2:"+para2+",para3:"+para3); return null; } }
5.调用接口的文件(axis实现)
TestByTestService.java
package com.test.ws.client; import java.net.MalformedURLException; import java.net.URL; import java.rmi.RemoteException; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; public class TestByTestService { public TestByTestService(){ } public static void main(String[] args) throws MalformedURLException, RemoteException, ServiceException{ for(int i=0;i<1;i++){ System.out.println("TestByTestService 第"+(i+1)+"次调用"); fun(); } } public static void fun() throws ServiceException, MalformedURLException, RemoteException{ String endpoint = "http://localhost:8080/ws/services/TestService?wsdl"; //ws为项目名称 String namespace = "http://server.ws.test.com/"; Service service = new Service(); Call call = (Call)service.createCall(); // 设置调用服务地址 call.setTargetEndpointAddress(new URL(endpoint)); // 此处一定要配置wsdl的namespace参数和调用方法名 call.setOperationName(new QName(namespace, "testFunc")); call.addParameter("arg0", XMLType.XSD_STRING, ParameterMode.IN); call.addParameter("arg1", XMLType.XSD_STRING, ParameterMode.IN); call.addParameter("arg2", XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType(XMLType.XSD_STRING); Object xml= call.invoke(new Object[]{"1","2","3"}); System.out.println("fun result :"+xml); } }
启用应用后,运行TestByTestService文件,会显示 para1:1,para2:2,para3:3,调用成功