使用cxf发布webservice
cxf版本和jdk版本
jdk版本 | CXF版本 |
---|---|
java 6 | 2.2.10 |
java 7 | 2.2.x |
java 8 | 3.x |
java 9+ | 3.3.x |
maven依赖
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<cxf.version>2.7.18</cxf.version>
</properties>
<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>
java接口和实现类
package com.sky.cxf01.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface HelloService {
@WebMethod
@WebResult
String SayHello(@WebParam String msg);
}
package com.sky.cxf01.service.impl;
import com.sky.cxf01.service.HelloService;
public class HelloServiceImpl implements HelloService {
@Override
public String SayHello(String msg) {
return "Hello, " + msg;
}
}
发布服务
package com.sky.cxf01.server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import com.sky.cxf01.service.impl.HelloServiceImpl;
public class Server {
public static void main(String[] args) throws InterruptedException {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(HelloServiceImpl.class);
factory.setAddress("http://localhost:9000/ws/HelloWorld");
factory.create();
System.out.println("start...");
Thread.sleep(60 * 1000);
System.out.println("Server exit...");
System.exit(-1);
}
}
生成客户端代码测试
https://blog.youkuaiyun.com/qq_26264237/article/details/97040977
客户类测试
package com.sky.cxf01.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import com.sky.cxf01.service.HelloService;
public class Client {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(HelloService.class);
factory.setAddress("http://localhost:9000/ws/HelloWorld");
HelloService service = (HelloService) factory.create();
System.err.println(service.SayHello("GG"));
}
}
启动服务后访问:http://localhost:9000/ws/HelloWorld?wsdl