当前位置 : 主页 > 网页制作 > Nodejs >

Webservice的cxf开发_入门(jdk1.7)

来源:互联网 收集:自由互联 发布时间:2021-06-24
一、部署cxf+ant环境 ant:http://download.csdn.net/detail/qwkxq/9706182 cxf:http://download.csdn.net/detail/qwkxq/9706179 下载以后解压,目录结构如下 配置环境变量: ANT_HOME=D:\PATH\apache-ant-1.8.4 CXF_HOME=D:\PATH

一、部署cxf+ant环境

ant:http://download.csdn.net/detail/qwkxq/9706182

cxf:http://download.csdn.net/detail/qwkxq/9706179

下载以后解压,目录结构如下


配置环境变量:

ANT_HOME=D:\PATH\apache-ant-1.8.4

CXF_HOME=D:\PATH\apache-cxf-2.4.2

CATALINA_HOME=D:\PATH\apache-tomcat-6.0.18\apache-tomcat-6.0.18

JAVA_HOME=C:\Java7

PATH=%CXF_HOME%\bin;%CATALINA_HOME%\bin;%ANT_HOME%\bin;%JAVA_HOME%\bin;

CLASSPATH=.;%CXF_HOME%\lib\cxf-manifest.jar;.\build\classes


cxf中常用的ant命令

“ant” 读取默认的build.xml文件构建项目(编译)

“ant server” 启动webservice服务端

“ant client”  启动webservice客户端调用服务端

“ant deploy -Dtomcat=true” 将服务端程序部署到tomcat,默认访问的是一个servlet,映射所有services/*的路径,访问地址是:“localhost:8080/项目名/services”

“ant undeploy -Dtomcat=true” 卸载部署在tomcat的服务端程序

“ant clean” 清空项目


二、开发cxf入门程序

1.服务端接口


package cn.xt.test;

import javax.jws.WebService;
import javax.xml.ws.BindingType;

@WebService
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public interface IMyService {
	public void sayHi();
}


2.服务端实现类


package cn.xt.test;

import java.util.Date;

import javax.jws.WebService;
import javax.xml.ws.BindingType;

@WebService
@BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
public class MyService implements IMyService {
	public void sayHi(){
		System.out.println("hi......"+new Date().toLocaleString());
	}
}

这里的@BindingType有2个常用的值:

javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_BINDING:发布soap1.1版本的服务端

javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING:发布soap1.2版本的服务端


3.发布服务


public static void main(String[] args) {
		IMyService service = new MyService();
		JaxWsServerFactoryBean bean = new JaxWsServerFactoryBean();
		bean.setAddress("http://192.168.1.3:9999/hello");
		bean.setServiceClass(IMyService.class);
		bean.setServiceBean(service);
		
		//添加cxf拦截器
		bean.getInInterceptors().add(new LoggingInInterceptor());
		bean.getOutInterceptors().add(new LoggingOutInterceptor());
		
		bean.create();
	}

访问http://192.168.1.3:9999/hello?wsdl出现wsdl说明,则说明发布成功

服务端控制台会有如下打印


信息: Inbound Message
----------------------------
ID: 6
Address: http://192.168.1.3:9999/hello?wsdl
Http-Method: GET
Content-Type: 
Headers: {Accept=[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8], accept-encoding=[gzip, deflate, sdch], Accept-Language=[zh-CN,zh;q=0.8], Cache-Control=[max-age=0], connection=[keep-alive], Content-Type=[null], Host=[192.168.1.3:9999], Upgrade-Insecure-Requests=[1], User-Agent=[Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36]}
--------------------------------------


4.使用cxf自带命令“wsdl2java” 生成客户端代码

wsdl2java -d . http://192.168.1.3:9999/hello?wsdl




5.调用


public static void main(String[] args) {
		IMyService service = new IMyServiceService().getIMyServicePort();
		service.sayHi();
	}
网友评论