WebService需要服务端和客户端构建多模块项目,为了方便此处以服务端为主项目
服务端:
1. pom.xml
<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.lyj</groupId>
<artifactId>WebServiceCXFTest1</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>WebServiceCXFClient1</module>
</modules>
<packaging>pom</packaging>
<name>WebServiceCXFTest1</name>
<url>http://maven.apache.org</url>
<properties>
<cxf.version>3.1.7</cxf.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</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</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>
</project>
2.编写接口、实例和发布接口
(1)
package com.lyj.ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* Created by LYJ on 2016/10/5.
*/
@WebService
public interface HelloWS {
@WebMethod
public String sayHello(String name);
}
(2)
package com.lyj.ws;
import javax.jws.WebService;
/**
* Created by LYJ on 2016/10/5.
*/
@WebService
public class HelloWSImpl implements HelloWS{
public String sayHello(String name) {
System.out.println( name+" 服务端--------- --- HelloWSImpl.sayHello\n");
return "hello "+name;
}
}
(3)
package com.lyj.test;
import com.lyj.ws.HelloWSImpl;
import javax.xml.ws.Endpoint;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args ){
String address="http://localhost:9090/cxf";
Endpoint.publish(address,new HelloWSImpl());
System.out.println("发布webservice成功");
}
}
打开浏览器输入http://localhost:9090/cxf?wsdl得到如下图所示就说明已成功开启WebService
这样我们就构建好了一个基于CXF的WebService,运行main方法(一直运行),以便我们编写客户端
客户端
1.pom.xml
<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">
<parent>
<artifactId>WebServiceCXFTest1</artifactId>
<groupId>com.lyj</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>WebServiceCXFClient1</artifactId>
<packaging>jar</packaging>
<name>WebServiceCXFClient1</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-codegen-plugin</artifactId>
<version>3.1.7</version>
<executions>
<execution>
<id>generate-sources</id>
<phase>generate-sources</phase>
<configuration>
<!--源码生成地址-->
<sourceRoot>${project.build.sourceDirectory}</sourceRoot>
<wsdlOptions>
<wsdlOption>
<wsdl>http://localhost:9090/cxf?wsdl</wsdl>
</wsdlOption>
</wsdlOptions>
</configuration>
<goals>
<goal>wsdl2java</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.运行generate-sources即可生成wsdl对应的java文件
3.编写测试文件
package com.lyj.test;
import com.lyj.ws.HelloWS;
import com.lyj.ws.HelloWSImplService;
public class App
{
public static void main( String[] args ){
HelloWSImplService factory=new HelloWSImplService();
HelloWS helloWS=factory.getHelloWSImplPort();
String result =helloWS.sayHello("tom");
System.out.println(result + " client--------- result --- App.main\n");
}
}