1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > SpringBoot项目中使用CXF发布和调用webservice接口

SpringBoot项目中使用CXF发布和调用webservice接口

时间:2019-09-11 02:53:46

相关推荐

SpringBoot项目中使用CXF发布和调用webservice接口

文章目录

1. 服务端发布webservice接口1.1. 引入依赖1.2. 编写接口1.3. 实现接口1.4. 发布webservice接口1.5. 发布多个webservice接口1.6. 测试1.7. 问题2. 客户端调用webservice接口2.1. 问题

1. 服务端发布webservice接口

1.1. 引入依赖

<!-- CXF webservice --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.12</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.1.12</version></dependency><!-- CXF webservice -->

1.2. 编写接口

package com.hdn.webservice.service;import javax.jws.WebMethod;import javax.jws.WebService;//@WebService用于对接口,类进行注解,表示要发布的web服务@WebServicepublic interface WS {//@WebMethod 注释表示作为一项 Web Service 操作的方法,此外 仅支持在使用 @WebService 注释来注释的类上使用 @WebMethod 注释@WebMethodString getMsg();}

@WebService用于对接口,类进行注解,表示要发布的web服务

@WebMethod注释表示作为一项 Web Service 操作的方法,此外 仅支持在使用 @WebService 注释来注释的类上使用 @WebMethod 注释

@WebParam(name = “param”)如果接口需要参数,就要使用@WebParam注解

1.3. 实现接口

package com.hdn.webservice.service;import javax.jws.WebService;//targetNamespace是指定你想要的名称空间,一般是使用接口实现类的包名//endpointInterface是服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口@WebService(targetNamespace = "http://com.hdn.webservice.service/",endpointInterface = "com.hdn.webservice.service.WS")public class WSImpl implements WS {public String getMsg() {return "调用到了webservice接口";}}

targetNamespace是指定你想要的名称空间,一般是使用接口实现类的包名

endpointInterface是服务接口全路径, 指定做SEI(Service EndPoint Interface)服务端点接口

1.4. 发布webservice接口

package com.hdn.webservice.service;import org.apache.cxf.Bus;import org.apache.cxf.bus.spring.SpringBus;import org.apache.cxf.jaxws.EndpointImpl;import org.apache.cxf.transport.servlet.CXFServlet;import org.springframework.boot.web.servlet.ServletRegistrationBean;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import javax.xml.ws.Endpoint;@Configurationpublic class WSConfig {//默认servlet路径/*,如果覆写则按照自己定义的来@Bean("dispatcherServletCxf")public ServletRegistrationBean wsDispatcherServlet(){return new ServletRegistrationBean(new CXFServlet(),"/businessSystem/services/*");//发布服务名称}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}//把实现类交给spring管理@Beanpublic WS webService() {return new WSImpl();}//终端路径@Beanpublic Endpoint endpoint() {EndpointImpl endpoint = new EndpointImpl(springBus(),webService());endpoint.publish("/testWS");return endpoint;}}

1.5. 发布多个webservice接口

如果需要发布多个webservice接口,就如同上面分别编写接口、实现接口、然后在WSConfig中进行webservice的发布即可。

1.6. 测试

浏览器中输入:127.0.0.1:8080/businessSystem/services/ (该路径是在WSConfig中定义的):

点击 {http://com.hdn.webservice.service/}WSImplService 就可以访问wsdl文件:

到这里,我们的webservice接口就已经成功发布了!!!

可以使用soupui工具来调用webservice接口。

1.7. 问题

在WSConfig中发布服务名称时,会报如下错误:

解决办法:

这是因为没有引入spring-boot-starter-web依赖,在pom.xml中引入该依赖即可:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>

2. 客户端调用webservice接口

package com.hdn.webservice.client;import org.apache.cxf.endpoint.Client;import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;public class webserviceclient {public static void main(String[] args) throws Exception {JaxWsDynamicClientFactory dcflient=JaxWsDynamicClientFactory.newInstance();//webservice地址Client client=dcflient.createClient("http://127.0.0.1:8080/businessSystem/services/testWS?wsdl");Object[] objects;System.out.println("hello !");try {//方法名 参数1 参数2 ...objects = client.invoke("getMsg");System.out.println("返回结果:"+objects[0].toString());} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();System.out.println("出错了"+e.toString());}}}

2.1. 问题

在使用客户端调用webervice的时候,服务端提供的webservice接口上@WebService注解必须添加targetNamespace,否则会报如下错误:

解决办法:

在服务端提供的webservice接口上@WebService注解添加targetNamespace:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。