springboot整合cxf发布webservice以及调用的方法
webservice性能不高,但是现在好多公司还是在用,恰好今天在开发的时候对接项目组需要使用到webservice下面来说下简单的案例应用
首先老规矩:引入jar包
org.apache.cxf cxf-spring-boot-starter-jaxws 3.1.11
新增一个公共的返回实体类
publicclassBaseResult{ privateStringisSuccess; privateStringerrCode; privateStringmessage; publicStringgetIsSuccess(){ returnisSuccess; } publicvoidsetIsSuccess(StringisSuccess){ this.isSuccess=isSuccess; } publicStringgetErrCode(){ returnerrCode; } publicvoidsetErrCode(StringerrCode){ this.errCode=errCode; } publicStringgetMessage(){ returnmessage; } publicvoidsetMessage(Stringmessage){ this.message=message; } }
其他类继承即可
@XmlRootElement(name="testResult") publicclassTestResultextendsBaseResultimplementsSerializable{ privatestaticfinallongserialVersionUID=-7128575337024823798L; privateListdata; publicList getData(){ returndata; } publicvoidsetData(List data){ this.data=data; } }
新增user类
publicclassUser{ privateStringname; privateintage; publicUser(Stringname,intage){ super(); this.name=name; this.age=age; } publicUser(){ super(); } publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicintgetAge(){ returnage; } publicvoidsetAge(intage){ this.age=age; } }
接下来新增服务接口
@WebService publicinterfaceTestWService{ @WebMethod @WebResult TestResultlist3(); }
实现服务接口
@Service @WebService(targetNamespace="http://ws.**.com/",//命名空间,一般是接口的包名倒序) endpointInterface="com.**.ws.TestWSservice")//接口全路径 //**自己改自己的包路径 publicclassTestWSserviceImplimplementsTestWSservice{ @Override publicTestResultlist3(){ Listlist=newArrayList (); list.add(newUser("张三",23)); list.add(newUser("李四",24)); TestResulttestResult=newTestResult(); testResult.setIsSuccess("Y"); testResult.setData(list); testResult.setMessage("操作成功"); returntestResult; } }
新增配置类,发布服务
importjavax.xml.ws.Endpoint; importorg.apache.cxf.Bus; importorg.apache.cxf.bus.spring.SpringBus; importorg.apache.cxf.jaxws.EndpointImpl; importorg.apache.cxf.transport.servlet.CXFServlet; importorg.springframework.beans.factory.annotation.Autowired; importorg.springframework.boot.web.servlet.ServletRegistrationBean; importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importcom.hikvision.hikserviceassign.ws.MonitorSurveyWS; importcom.hikvision.hikserviceassign.ws.SmartLockServiceOrderWS; /** *webservice发布服务类 *@authorXupx *@Date2018年8月14日下午4:25:25 * */ @Configuration publicclassCxfConfig{ @Autowired privateTestWServicetestWService; @SuppressWarnings("all") @Bean publicServletRegistrationBeanwsServlet(){ ServletRegistrationBeanbean=newServletRegistrationBean(newCXFServlet(),"/soap/*"); returnbean; } @Bean(name=Bus.DEFAULT_BUS_ID) publicSpringBusspringBus(){ returnnewSpringBus(); } @Bean publicEndpointtestWService(){ //会找到O2oWebService的实现类,所以实现类只能有一个 EndpointImplendpoint=newEndpointImpl(springBus(),testWService); endpoint.publish("/testWService"); returnendpoint; } }
启动项目,然后打开路径:localhost:8080/soap可以查看多个自己发布的服务,如果要发布多个服务,使用多个Bean即可
测试调用1:
importorg.apache.cxf.endpoint.Client; importorg.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; importorg.junit.Test; importorg.junit.runner.RunWith; importorg.springframework.boot.test.context.SpringBootTest; importorg.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest publicclassSpringBootCxfApplicationTests{ @Test publicvoidcontextLoads()throwsException{ JaxWsDynamicClientFactorydcf=JaxWsDynamicClientFactory.newInstance(); Clientclient=dcf.createClient("http://127.0.0.1:8080/soap/testWservice?wsdl"); Object[]objects=client.invoke("list3",param1,param2);//list3方法名后面是可变参数 //输出调用结果 System.out.println(objects[0].getClass()); System.out.println(objects[0].toString()); } }
客户端调用,用soapUI生成客户端(具体方法自己百度下,不介绍了)
TestWSImplServiceimplService=newTestWSImplService(); TestServiceWSws=implService.getTestServiceWSImplPort(); TestResultresult=ws.list3(); System.err.println(result);
增加密码校验,以下基础内容引用https://www.nhooo.com/article/145707.htm,我补充下包依赖
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; publicclassLoginInterceptorextendsAbstractPhaseInterceptor{ privateStringusername="root"; privateStringpassword="admin"; publicLoginInterceptor(Stringusername,Stringpassword){ //设置在发送请求前阶段进行拦截 super(Phase.PREPARE_SEND); this.username=username; this.password=password; } @Override publicvoidhandleMessage(SoapMessagesoapMessage)throwsFault{ List headers=soapMessage.getHeaders(); Documentdoc=DOMUtils.createDocument(); Elementauth=doc.createElementNS("http://cxf.wolfcode.cn/","SecurityHeader"); ElementUserName=doc.createElement("username"); ElementUserPass=doc.createElement("password"); UserName.setTextContent(username); UserPass.setTextContent(password); auth.appendChild(UserName); auth.appendChild(UserPass); headers.add(0,newHeader(newQName("SecurityHeader"),auth)); } }
importjava.util.List; importjavax.xml.soap.SOAPException; importorg.apache.commons.lang3.StringUtils; importorg.apache.cxf.binding.soap.SoapHeader; importorg.apache.cxf.binding.soap.SoapMessage; importorg.apache.cxf.interceptor.Fault; importorg.apache.cxf.phase.AbstractPhaseInterceptor; importorg.apache.cxf.phase.Phase; importorg.w3c.dom.Element; importorg.w3c.dom.NodeList; importorg.apache.cxf.headers.Header; publicclassAuthInterceptorextendsAbstractPhaseInterceptor{ privatestaticfinalStringUSERNAME="root"; privatestaticfinalStringPASSWORD="admin"; publicAuthInterceptor(){ //定义在哪个阶段进行拦截 super(Phase.PRE_PROTOCOL); } @Override publicvoidhandleMessage(SoapMessagesoapMessage)throwsFault{ List headers=null; Stringusername=null; Stringpassword=null; try{ headers=soapMessage.getHeaders(); }catch(Exceptione){ e.printStackTrace(); } if(headers==null){ thrownewFault(newIllegalArgumentException("找不到Header,无法验证用户信息")); } //获取用户名,密码 for(Headerheader:headers){ SoapHeadersoapHeader=(SoapHeader)header; Elemente=(Element)soapHeader.getObject(); NodeListusernameNode=e.getElementsByTagName("username"); NodeListpwdNode=e.getElementsByTagName("password"); username=usernameNode.item(0).getTextContent(); password=pwdNode.item(0).getTextContent(); if(StringUtils.isEmpty(username)||StringUtils.isEmpty(password)){ thrownewFault(newIllegalArgumentException("用户信息为空")); } } //校验用户名密码 if(!(username.equals(USERNAME)&&password.equals(PASSWORD))){ SOAPExceptionsoapExc=newSOAPException("认证失败"); thrownewFault(soapExc); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。