CXF 简介
Apache CXF = Celtix + XFire
Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。CXF 继承了 Celtix 和 XFire 两大开源项目的精华,提供了对 JAX-WS 全面的支持,并且提供了多种 Binding 、DataBinding、Transport 以及各种 Format 的支持,并且可以根据实际项目的需要,采用代码优先(Code First)或者 WSDL 优先(WSDL First)来轻松地实现 Web Services 的发布和使用。Apache CXF 已经是一个正式的 Apache 顶级项目。
官网:http://cxf.apache.org/
创建 Maven 工程
pom.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 <?xml version="1.0" encoding="UTF-8" ?> <project xmlns ="http://maven.apache.org/POM/4.0.0" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelVersion > 4.0.0</modelVersion > <groupId > com.example</groupId > <artifactId > webservice</artifactId > <version > 1.0.0-SNAPSHOT</version > <packaging > war</packaging > <properties > <project.build.sourceEncoding > UTF-8</project.build.sourceEncoding > <java.version > 1.8</java.version > <cxf.version > 3.3.4</cxf.version > </properties > <dependencies > <dependency > <groupId > org.apache.cxf</groupId > <artifactId > cxf-rt-frontend-jaxws</artifactId > <version > ${cxf.version}</version > </dependency > <dependency > <groupId > org.apache.cxf</groupId > <artifactId > cxf-rt-transports-http-jetty</artifactId > <version > ${cxf.version}</version > </dependency > </dependencies > <build > <plugins > <plugin > <groupId > org.apache.maven.plugins</groupId > <artifactId > maven-compiler-plugin</artifactId > <version > 3.7.0</version > <configuration > <source > ${java.version}</source > <target > ${java.version}</target > <encoding > ${project.build.sourceEncoding}</encoding > <showWarnings > true</showWarnings > </configuration > </plugin > </plugins > <resources > <resource > <directory > src/main/java</directory > <excludes > <exclude > **/*.java</exclude > </excludes > </resource > <resource > <directory > src/main/resources</directory > </resource > </resources > </build > </project >
WebService 服务端接口
WebServiceI 接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 @WebService public interface WebServiceI { @WebMethod String sayHello (String name) ; @WebMethod(operationName = "webserviceSave") String save (String name, String pwd) ; }
WebServiceImpl 实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 @WebService public class WebServiceImpl implements WebServiceI { @WebMethod @Override public String sayHello (String name) { System.out.println("WebService sayHello:" + name); return "sayHello" + name; } @WebMethod(operationName = "webserviceSave") @Override public String save (String name, String pwd) { System.out.println("WebService save:" + name + "," + pwd); return "save Success" ; } }
发布方式
用 JaxWsServerFactoryBean 类发布
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public class WebServicePublish { public static void main (String[] args) { JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean (); jaxWsServerFactoryBean.setAddress("http://127.0.0.1:8989/webservice/cxf" ); jaxWsServerFactoryBean.setServiceClass(WebServiceI.class); jaxWsServerFactoryBean.setServiceBean(new WebServiceImpl ()); jaxWsServerFactoryBean.create(); System.out.println("发布 webservice 成功!" ); } }
用 ServletContextListener 监听器发布
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 @WebListener public class WebServicePublishListener implements ServletContextListener { @Override public void contextInitialized (ServletContextEvent sce) { JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean (); jaxWsServerFactoryBean.setAddress("http://127.0.0.1:8080/webservice/cxf" ); jaxWsServerFactoryBean.setServiceClass(WebServiceI.class); jaxWsServerFactoryBean.setServiceBean(new WebServiceImpl ()); jaxWsServerFactoryBean.create(); System.out.println("发布 webservice 成功!" ); } @Override public void contextDestroyed (ServletContextEvent sce) { } }
用 Servlet 发布
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 @WebServlet(value = "", loadOnStartup = 0) public class WebServicePublishServlet extends HttpServlet { @Override public void init () throws ServletException { JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean (); jaxWsServerFactoryBean.setAddress("http://127.0.0.1:8888/webservice/cxf" ); jaxWsServerFactoryBean.setServiceClass(WebServiceI.class); jaxWsServerFactoryBean.setServiceBean(new WebServiceImpl ()); jaxWsServerFactoryBean.create(); System.out.println("发布 webservice 成功!" ); } }
案例源码:https://github.com/V-Vincen/webservice
参考:https://blog.csdn.net/hgx_suiyuesusu/article/details/88918192
If you like this blog or find it useful for you, you are welcome to comment on it. You are also welcome to share this blog, so that more people can participate in it. If the images used in the blog infringe your copyright, please contact the author to delete them. Thank you !