1. 配置Apache CXF
- 下载 Apache CXF:去官网 http://cxf.apache.org/download.html 下载
- 配置环境变量
2.创建服务端之前先创建一个maven父工程,方便服务端和客户端maven工程的创建和jar包依赖以及插件的集成配置等。
2.1 新建maven工程 ws_parent,其pom配置如下
<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.lonely.webservice</groupId> <artifactId>ws_parent</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <!-- 配置依赖 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.2.1</version> </dependency> </dependencies> </dependencyManagement> <!-- 配置插件信息 --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
3.创建服务端
3.1 创建一个maven子工程 ws_server,继承父工程,
在父工程的pom文件中添加如下配置
<modules> <module>ws_server</module> </modules>在子工程中的pom文件添加如下配置
<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> <parent> <groupId>com.lonely.webservice</groupId> <artifactId>ws_parent</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>ws_server</artifactId> <!-- 添加依赖 --> <dependencies> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> </dependency> </dependencies> </project>3.2 创建一个服务接口
package com.lonely.server;
import javax.jws.WebService;
@WebService
public interface HelloWs {
public String sayHelloWs(String str);
}
3.3 创建该接口的实现类package com.lonely.server.impl; import javax.jws.WebService; import com.lonely.server.HelloWs; @WebService public class HelloWsImpl implements HelloWs { @Override public String sayHelloWs(String str) { return str + ":正在学习web Service~~~~~~~~~~~~~~"; } }3.4 创建一个服务发布类
package com.lonely.server.impl; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.lonely.server.HelloWs; public class ReleaseClient { public static void main(String[] args) { System.out.println("WS 服务端 start~~~~~~"); String address = "http://localhost:8090/sayhello"; HelloWs helloWs = new HelloWsImpl(); JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean(); // 设置地址 jaxWsServerFactoryBean.setAddress(address); // 设置接口 jaxWsServerFactoryBean.setServiceClass(HelloWs.class); // 设置实现类 jaxWsServerFactoryBean.setServiceBean(helloWs); jaxWsServerFactoryBean.create(); System.out.println("WS 服务端 started~~~~~~~"); } }3.5 至此,我们启动该类,输入发布地址然后加上 ?wsdl 查看效果