webservciescxf框架之客户端与服务端实例详解

webservciescxf框架之客户端与服务端实例详解

可以关注我之前发的文章,那是采用jdk发布服务并且使用wsimpor来生成客户端的。

但本文采用的是soap1.2协议,而wsimport仅对soap1.1协议有效,所以,本文采用的是

cxf框架提供的wsdl2java 来生成客户端,如下:

wsdl2java -d . http://127.0.0.1/framework?wsdl

另外,需要强调的是wsdl2java工具(axis好像也提供了)既支持soap1.1协议,也支持soap1.2协议,生成客户端代码。

如图:

1)服务端cxfservices:

package com.neusoft.si;

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

@WebService @BindingType(value=javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)

public class cxfservices {  

public String sayHello(String name){   return "hello:"+name;  

}

}

2)  发布服务端server

package com.neusoft.si;

import org.apache.cxf.interceptor.LoggingInInterceptor;

import org.apache.cxf.interceptor.LoggingOutInterceptor;

import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class mycxfserver {  

public static void main(String[] args) {  

 //soap1.1---wsimpot可以使用   

//soap1.1  ,  soap1.2---wsdl2java -d . wsdl路径   

JaxWsServerFactoryBean server=new JaxWsServerFactoryBean();  

//客户端调用时,打印输入输出日志

 server.getInInterceptors().add(new LoggingInInterceptor());   

server.getOutInterceptors().add(new LoggingOutInterceptor());   

server.setAddress("http://127.0.0.1/framework");   

server.setServiceClass(cxfservices.class);   

server.setServiceBean(new cxfservices());   

server.create();   

System.out.println("server ready");  

}

}

3)客户端:

客户端生成代码根据wsdl2java生成下,此处不做展示。

package com.slrc;

import com.neusoft.si.Cxfservices;

import com.neusoft.si.CxfservicesService;

public class cxf_client {  

public static void main(String[] args) {  

 CxfservicesService service=new CxfservicesService();  

 Cxfservices  client=service.getCxfservicesPort();  

 String result=client.sayHello("jwe");   

System.out.println(result);

 }

}

猜你喜欢

转载自www.cnblogs.com/xiaohanjy/p/11183083.html