提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
WebService整合SpringBoot2.0
- 一、生产者
- 1、依赖
- 2、被调用方法
- 3、配置类
- 4、调用地址
- 5、启动项目
- 二、消费者
- 1、依赖
- 2、代码
- 3、效果
一、生产者
1、依赖
- spring-boot-starter-web-services:
这个没啥好说的吧,你springboot集成webservice,不加入这个包,难道加个什么quartz包? - cxf-spring-boot-starter-jaxws
这个包是给webservice发布使用的。
我们知道 Web Service是一种能够使应用程序在不同的平台使用不同的编程语言进行通讯的技术规范,这种技术规范的实现方式是通过基于XML形式的协议(SOAP)进行通讯或者说是RESTFUL形式的。
如果使用这两种方式进行通讯,那么必须要有规范化的工作,也就是jaxws规范和 jar-rs规范。
而cxf正好是这两种规范的具体实现方式。
换句话说:JAX-WS是标准,CXF与Axis则是具体的框架实现。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.2.4</version></dependency><dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>5.2.4.Final</version></dependency>
2、被调用方法
@WebService:标明是个webservice服务,发布的时候会带上这个类。
@WeBMethod:webservice中发布的方法
@WebParam:对参数的别名,不写也不影响,但是参数在wsdl中看起来是arg0,不利于理解。
package com.example.myspringboot2.service;import javax.jws.WebMethod;
import javax.jws.WebService;/*** @author lichangyuan* @create 2021-11-26 13:55*/
@WebService(targetNamespace = "http://myspringboot2.example.com/")// 由于不同包所以要在接口和实现类上各加一个targetNamespace ,命名空间,一般是接口的包名倒序)
public interface AobingService {@WebMethodString hello(String name);
}
package com.example.myspringboot2.service.impl;import com.example.myspringboot2.service.AobingService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;import javax.jws.WebService;/*** @author lichangyuan* @create 2021-11-26 13:57*/
@WebService(serviceName = "AobingService", // 与接口中指定的name一致targetNamespace = "http://myspringboot2.example.com/",// 命名空间,一般是接口的包名倒序endpointInterface = "com.example.myspringboot2.service.AobingService"// 接口地址
)
@Service
public class AobingServiceImpl implements AobingService {@Overridepublic String hello(String name) {return "Yo man Hello,I am" + name;}
}
3、配置类
package com.example.myspringboot2.config;import javax.xml.ws.Endpoint;import com.example.myspringboot2.service.AobingService;
import org.apache.cxf.Bus;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.apache.cxf.transport.servlet.CXFServlet;import javax.xml.ws.Endpoint;/*** @author lichangyuan* @create 2021-11-26 14:57*/
@Configuration
public class WebServiceConfig {@Autowiredprivate AobingService aobingService;// 此方法被注释后:wsdl访问地址为http://127.0.0.1:9090/webservice?wsdl// 去掉注释后:wsdl访问地址为:http://127.0.0.1:9090/webservice/aobingService?wsdl@Bean(name = "cxfServlet")public ServletRegistrationBean cxfServlet() {return new ServletRegistrationBean(new CXFServlet(), "/webservice/*");}@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}/*** 注册WebServiceDemoService接口到webservice服务** @return*/@Bean(name = "webServiceDemoEndPoint")public Endpoint webServiceDemoEndPoint() {EndpointImpl endpoint = new EndpointImpl(springBus(), aobingService);endpoint.publish("/aobingService");return endpoint;}}
4、调用地址
http://127.0.0.1:9090/webservice/aobingService?wsdl
方法详情
http://127.0.0.1:9090/webservice/aobingService?wsdl
5、启动项目
二、消费者
1、依赖
<dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.2.4</version></dependency>
2、代码
package com.example.myspringboot2.controller;import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;import javax.xml.namespace.QName;/*** @author lichangyuan* @create 2021-11-26 16:47*/
public class AobingGetController {public static final String URL = "http://127.0.0.1:9090/webservice/aobingService?wsdl";public static final String NAMESPACE_URL = "http://myspringboot2.example.com/";public static final String METHOD_NAME = "hello";public static final String PARAM_STR = "渊哥";public static void main(String[] args) {JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();// 创建动态客户端, URL -> 接口地址Client client = dcf.createClient(URL);// 创建QName, NAMESPACE_URL -> 命名空间,METHOD_NAME -> 方法名QName qName = new QName(NAMESPACE_URL, METHOD_NAME);try {// 接口调用 PARAM_STR -> xml参数字符串Object[] objects = client.invoke(qName, PARAM_STR);// 返回的数据System.out.println(objects[0].toString());} catch (Exception e) {e.printStackTrace();}}}