当前位置:

CXF整合SpringBoot实现

访客 2024-04-24 741 0

步骤一、pom文件

<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.2.4</version></dependency>

步骤二、服务端

importjavax.jws.WebService;importjava.util.HashMap;@WebService(serviceName="TeacherService",//对外发布的服务名targetNamespace="http://service.demo.example.com/",//指定你想要的名称空间,通常使用使用包名反转endpointInterface="com.example.demo.service.TeacherService")//服务接口全路径,指定做SEI(ServiceEndPointInterface)服务端点接口@ServicepublicclassTeacherServiceImplimplementsTeacherService{@OverridepublicObjectgetTeacherById(Longid){HashMap<String,Object>map=newHashMap<>();map.put("id",id);returnmap.toString();}}

步骤三、服务端配置文件

importcom.example.demo.service.TeacherService;importorg.apache.cxf.Bus;importorg.apache.cxf.jaxws.EndpointImpl;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjavax.xml.ws.Endpoint;@ConfigurationpublicclassCxfConfig{@AutowiredprivateBusbus;@AutowiredTeacherServiceteacherService;/***此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问*此方法被注释后:wsdl访问地址为http://127.0.0.1:8080/services/teacher?wsdl*去掉注释后:wsdl访问地址为:http://127.0.0.1:8080/soap/teacher?wsdl*@return*//*@SuppressWarnings("all")@BeanpublicServletRegistrationBeandispatcherServlet(){returnnewServletRegistrationBean(newCXFServlet(),"/soap/*");}*//**JAX-WS*站点服务***/@BeanpublicEndpointteacherEndpoint(){EndpointImplendpoint=newEndpointImpl(bus,teacherService);endpoint.publish("/teacher");returnendpoint;}}

步骤四、客户端

importcom.example.demo.service.TeacherService;importorg.apache.cxf.endpoint.Client;importorg.apache.cxf.jaxws.JaxWsProxyFactoryBean;importorg.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;/***该类提供两种不同的方式来调用webservice服务*1:代理工厂方式*2:动态调用webservice*/publicclassCxfClient{publicstaticvoidmain(String[]args){refWebService("http://127.0.0.1:8080/services/teacher?wsdl","getTeacherById",1L);}/***1.代理类工厂的方式,需要拿到对方的接口地址*address:http://127.0.0.1:8080/soap/teacher?wsdl*paramJson:maple*/publicstaticObjectproxyWebService(Stringaddress,Objectargs){try{//代理工厂JaxWsProxyFactoryBeanjaxWsProxyFactoryBean=newJaxWsProxyFactoryBean();//设置代理地址jaxWsProxyFactoryBean.setAddress(address);//设置接口类型jaxWsProxyFactoryBean.setServiceClass(TeacherService.class);//创建一个代理接口实现TeacherServiceservice=(TeacherService)jaxWsProxyFactoryBean.create();//调用代理接口的方法调用并返回结果Objecto=service.getTeacherById(Long.getLong(args.toString()));System.out.println(o);returno;}catch(Exceptione){e.printStackTrace();}returnnull;}/***2:动态调用*address:http://127.0.0.1:8080/soap/teacher?wsdl*method:getUserName*paramJson:maple*/publicstaticStringrefWebService(Stringaddress,Stringmethod,Object...args){//创建动态客户端JaxWsDynamicClientFactorydcf=JaxWsDynamicClientFactory.newInstance();Clientclient=dcf.createClient(address);//需要密码的情况需要加上用户名和密码//client.getOutInterceptors().add(newClientLoginInterceptor(USER_NAME,PASS_WORD));Object[]objects;try{//invoke("方法名",参数1,参数2,参数3....);objects=client.invoke(method,args);System.out.println("返回数据:"objects[0]);}catch(java.lang.Exceptione){e.printStackTrace();}returnnull;}}

步骤五、拦截器

importjava.util.List;importjavax.xml.namespace.QName;importorg.apache.cxf.binding.soap.SoapMessage;importorg.apache.cxf.headers.Header;importorg.apache.cxf.helpers.DOMUtils;importorg.apache.cxf.interceptor.Fault;importorg.apache.cxf.phase.AbstractPhaseInterceptor;importorg.apache.cxf.phase.Phase;importorg.w3c.dom.Document;importorg.w3c.dom.Element;/***类说明*用于調用webservices接口的安全验证拦截器*/publicclassClientLoginInterceptorextendsAbstractPhaseInterceptor<SoapMessage>{publicClientLoginInterceptor(StringuserName,StringuserPass){super(Phase.PREPARE_SEND);this.userName=userName;this.userPass=userPass;}privateStringuserName;privateStringuserPass;publicStringgetUserName(){returnuserName;}publicvoidsetUserName(StringuserName){this.userName=userName;}publicStringgetUserPass(){returnuserPass;}publicvoidsetUserPass(StringuserPass){this.userPass=userPass;}@OverridepublicvoidhandleMessage(SoapMessagesoap)throwsFault{List<Header>headers=soap.getHeaders();Documentdoc=DOMUtils.createDocument();Elementauth=doc.createElementNS("http://service.demo.example.com/","SecurityHeader");ElementuserName=doc.createElement("userName");ElementuserPass=doc.createElement("userPass");userName.setTextContent(this.userName);userPass.setTextContent(this.userPass);auth.appendChild(userName);auth.appendChild(userPass);headers.add(0,newHeader(newQName("SecurityHeader"),auth));}}

发表评论

  • 评论列表
还没有人评论,快来抢沙发吧~