一、cxf发布webservice接口
添加maven依赖
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>3.4.3</version>
</dependency>
这里的cxf-rt-transports-http-jetty依赖是内置jetty容器发布webservice接口
创建服务端接口
@WebService
public interface UserService {
String getUserInfo(String name);
}
@WebService
public class UserServiceImpl implements UserService {
@Override
public String getUserInfo(String name) {
return "我是" + name;
}
}
public class test {
public static void main(String[] args) {
System.out.println("web service start");
UserService implementor = new UserServiceImpl();
String address = "http://127.0.0.1:8082/userService";
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
factoryBean.setAddress(address); // 设置暴露地址
factoryBean.setServiceClass(UserService.class); // 接口类
factoryBean.setServiceBean(implementor); // 设置实现类
factoryBean.create();
System.out.println("web service started");
System.out.println("web service started");
}
}
启动main方法,正常情况会发布成功
浏览器访问http://127.0.0.1:8082/userService?wsdl 返回下面内容说明发布成功
二、客户端进行动态调用webservice接口
public static void main(String[] args) throws Exception {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client = dcf.createClient("http://127.0.0.1:8082/userService?wsdl");
// namespace是命名空间,methodName是方法名
QName name = new QName("http://service.test.example.com/", "getUserInfo");
Object[] objects = client.invoke(name, "我没收到反馈");
System.out.println(objects[0].toString());
}
正常情况调用接口会返回成功
cxf的动态调用不适合传递自定义对象,需要传递自定义对象,需要使用静态调用生成本地代码
三、cxf静态调用生成本地代码
采用apache-cxf-3.4.3版本,jar包可到官网下载 http://cxf.apache.org/download.html
使用JAX-WS Proxy代理工厂方式开发客户端,首先需要使用wsdl2java工具生成web service在本地的代理接口
1、配置环境变量
wsdl2java.bat在CXF根目录\bin目录下,使用之前需要配置CXF环境变量,CXF_HOME变量值为CXF根目录,并在path中添加%CXF_HOME%\bin,如:
2、wsdl2java生成代理接口
配置好环境变量后,打开CMD命令行窗口,将当前目录切换到java工程src目录下,命令行输入如下格式命令,回车,即可根据wsdl生成服务端的代理接口。
wsdl2java -p ws.cxf -client http://127.0.0.1:8082/userService?wsdl
命令说明:
-p 指定代理接口所在包名,-client(生成客户端) 后面的字符串声明了服务接口的wsdl
public static void test() {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(UserService.class);
factory.setAddress("http://127.0.0.1:8082/userService");
Object object = factory.create();
UserService service = (UserService) object;
UserVO userVO = new UserVO();
userVO.setName("李师傅打开");
String userInfo = service.getUserInfo(userVO);
System.out.println(userInfo);
}
3、第二种调用方法
ExecuteBindQSService executeBindQSService = new ExecuteBindQSService(ExecuteBindQSService.getWsdlLocation(WSDL_URL));
ExecutePtt executePtt = executeBindQSService.getExecuteBindQSPort();
//获取代理,并设置日志拦截器,打印日志
Client client = ClientProxy.getClient(executePtt);
client.getInInterceptors().add(new LoggingInInterceptor());
client.getOutInterceptors().add(new LoggingOutInterceptor());
// 超时时间设置
HTTPConduit http = (HTTPConduit) clientProxy.getConduit();
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
httpClientPolicy.setConnectionTimeout(Integer
.valueOf(webServiceConTimeout));
httpClientPolicy.setReceiveTimeout(Integer
.valueOf(webServiceRevTimeout));
httpClientPolicy.setAllowChunking(false);
http.setClient(httpClientPolicy);
//调用方法执行
Response response = executePtt.execute(request);
4、关闭元素校验
@EndpointProperty(key = “soap.no.validate.parts”, value = “true”)
四、使用的是apache的axis实现的
第二种访问webservice的方法是通过idea生成客户端代码,访问服务端接口向访问本地接口一样。使用的是apache的axis实现的。