WebService整合SpringBoot2.0

news/2023/9/27 7:27:10

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

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();}}}

3、效果

在这里插入图片描述


https://dhexx.cn/news/show-18240.html

相关文章

Linux线程学习(一)

一、Linux进程与线程概述 进程与线程 为什么对于大多数合作性任务&#xff0c;多线程比多个独立的进程更优越呢&#xff1f;这是因为&#xff0c;线程共享相同的内存空间。不同的线程可以存取内存中的同一个变量。所以&#xff0c;程序中的所有线程都可以读或写声明过的全局变量…

Hue概述,核心功能,架构说明

Hue简单介绍 HUEHadoop User Experience 个人理解&#xff1a;可视图的webui界面&#xff0c;方便大数据技术之间的CRUD操作。 官方定义&#xff1a;Hue是一个能够与Apache Hadoop交互的Web应用程序。一个开源的Apache Hadoop UI。 特性&#xff1a;一个HDFS的文件浏览器&am…

dubbo简单了解

dubbo从入门到实战一、简介1、特点2、为什么要用Dubbo3、Dubbo 和 Spring Cloud 有什么区别4、SpringCloud和Dubbo的RPC相关比较5、Dubbo服务注册与发现的流程图二、服务引用大致流程1、服务引入的三种方式三、常见问题1、什么是RPC2、为什么要有 RPC&#xff0c;HTTP 不好么3、…

easyui时间格式问题

问题描述: 前端使用EasyUI,后台使用Spring MVC, 数据库里面存储的时间格式为:2014-06-10,但是后台返回给前台页面的数据是json格式的,类似于:1402367297000的形式,日期列datebox是无法解析的。具体如下图: 自己也是EasyUI小白,网上查查资料,倒腾下总算搞出来了,这里做下记录。 …

hdu 2485(最小费用最大流)

题目链接&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid2485 思路&#xff1a;题目的意思是删除最少的点使1&#xff0c;n的最短路大于k。将点转化为边&#xff0c;容量为1&#xff0c;费用为0&#xff0c;然后就是对于那些有道路的城市之间连边&#xff0c;若&#…

MySQL索引优化进阶如聚集索引、辅助索引、覆盖索引、联合索引

MySQL的覆盖索引与回表和order by一、聚集索引二、覆盖索引与回表1、表结构2、覆盖索引3、回表三、辅助索引四、联合索引五、哪些场景可以利用索引覆盖来优化SQL1、 全表count查询优化2、 列查询回表优化3、分页查询六、其他索引优化1、带条件查询索引优化2、一般索引优化3、or…

服务器配置MySQL主从复制搭建,一主多从

服务器配置MySQL主从复制搭建&#xff0c;一主多从一、前期准备1、目标效果效果二、配置步骤1、环境2、数据与自定义账号3、配置主数据库4、配置从数据库三、测试1、主数据库测试样例一、前期准备 1、目标效果效果 目标&#xff1a;搭建三台MySQL服务器&#xff0c;一台作为主…

Hue 集成HDFS文件系统

Hue 集成HDFS 注意修改完HDFS相关配置后&#xff0c;需要把配置scp分发给集群中每台机器&#xff0c;重启hdfs集群。 1.1&#xff0e; 修改core-site.xml配置 <!—允许通过httpfs方式访问hdfs的主机名 --> <property> <name>hadoop.proxyuser.root.hosts&…

SpringBoot 实现定时任务的两种方式:基于注解(@Scheduled)的简单定时器,基于接口SchedulingConfigurer的动态定时任务

SpringBoot 实现定时任务的两种方式一、cron表达式语法二、Scheduled注解使用1、代码2、效果三、SchedulingConfigurer接口1、代码2、效果四、进阶版基于接口SchedulingConfigurer的动态定时任务1、代码2、效果一、cron表达式语法 cron表达式语法:[秒] [分] [小时] [日] [月] …

hue集成yarn

Hue集成YARN 2.1&#xff0e; 修改hue.ini [[yarn_clusters]][[[default]]]resourcemanager_hostnode-1resourcemanager_port8032submit_toTrueresourcemanager_api_urlhttp://node-1:8088history_server_api_urlhttp://node-1:198882.2&#xff0e; 开启yarn日志聚集服务 M…