注: 该篇主要介绍使用 CXF 创建WebService,若欲了解 AXIS 或 Jaxws,请移步 AXIS 创建WebService 或Jaxws 创建WebService
#1. 创建接口和实现
@WebService public interface IService { public String login(String name,String pwd); } @WebService(endpointInterface="ws.IService") public class ServiceImpl implements IService{ @Override public String login(String name, String pwd) { if(name.equals("123") && pwd.equals("456")){ return "登录成功"; }else{ return "用户名或密码失败"; } } }
#2. 发布服务(配置 web.xml 和 applicationContext.xml 文件)
a. 配置 web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <display-name>springcxfws</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/ws/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
b. 配置 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" 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"> <!-- 下面文件需要在低版本的CXF时引入 --> <!-- <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="service" implementor="ws.ServiceImpl" address="/IService"> </jaxws:endpoint> </beans>
#3. 调用(本地调用和远程调用)
a. 本地调用<1>
public class LocalClient { public static void main(String[] args) { JaxWsProxyFactoryBean jaxws = new JaxWsProxyFactoryBean(); jaxws.setServiceClass(IService.class); jaxws.setAddress("http://localhost:8080/ws/IService"); IService service = (IService)jaxws.create(); System.out.println(service.login("123", "123")); System.out.println(service.login("123", "456")); } }
a. 本地调用<2>
添加spring配置
然后调用<!-- 两种方式任选一种即可 --> <!-- 客户端生成配置第1种方式 @start@ --> <!-- <bean id="localClient" class="ws.IService" factory-bean="localClientFactory" factory-method="create"/> <bean id="localClientFactory" class="org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> <property name="serviceClass" value="ws.IService"/> <property name="address" value="http://localhost:8080/ws/IService"/> </bean> --> <!-- 客户端生成配置第1种方式 @end@ --> <!-- 客户端生成配置第2种方式 可配置 cxf 拦截器,对消息进行特殊处理 @start@--> <jaxws:client id="localClient" serviceClass="ws.IService" address="http://localhost:8080/ws/IService"> </jaxws:client>
public class LocalClient { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("client.xml"); IService ser = (IService)context.getBean("localClient"); System.out.println("调用接口方法start"); System.out.println(ser.login("123", "123")); System.out.println("调用接口方法end"); // System.out.println(ser.login("123", "456")); context.close(); } }
b. 远程调用
使用 cxf 的 wsdl2java 命令生成客户端,服务器端等文件.
用法: wsdl2java [options] <wsdl_url>
参数: -p生成代码包名,-d 指定生成代码目录 ,-client 生成客户端代码 ,-server 生成服务器代码,-impl 生成实现代码,-ant 生成build.xml文件,-all 生成所有相关文件
示例: wsdl2java -d java源文件目录\src -all -keep -verbose http://localhost:8080/ws/IService?wsdl
执行上述命令后调用服务
如欲了解CXF基于JAVA,请移步 CXF 创建 WebService (基于 JAVA)public class RemoteClient { public static void main(String[] args) { ServiceImplService ser = new ServiceImplService(); IService service = ser.getPort(IService.class); System.out.println(service.login("123", "123")); System.out.println(service.login("123", "456")); } }