spring boot 集成webservice soap发短信【干货踩坑、亲测可用、干就完了】

2024-08-29 19:04

本文主要是介绍spring boot 集成webservice soap发短信【干货踩坑、亲测可用、干就完了】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说明:springboot版本 2.7.11

1.pom

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web-services</artifactId><version>2.7.11</version></dependency><!-- apache-cxf --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>3.1.6</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>3.1.6</version></dependency><!-- apache-cxf  此处与上处二选其一 --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-spring-boot-starter-jaxws</artifactId><version>3.5.2</version></dependency>

2.config

package com.ruoyi.config;import com.ruoyi.webservice.SmsService;
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.annotation.Resource;
import javax.xml.ws.Endpoint;/*** @author: zjl* @datetime: 2024/3/29* @desc:*/
@Configuration
public class WebServiceConfig {@Resourceprivate SmsService smsService;/*    @Resourceprivate AgeInfoService ageInfoService;*/
/*    @Resourceprivate WebServiceAuthInterceptor interceptor;*//*** Apache CXF 核心架构是以BUS为核心,整合其他组件。* Bus是CXF的主干, 为共享资源提供一个可配置的场所,作用类似于Spring的ApplicationContext,这些共享资源包括* WSDl管理器、绑定工厂等。通过对BUS进行扩展,可以方便地容纳自己的资源,或者替换现有的资源。默认Bus实现基于Spring架构,* 通过依赖注入,在运行时将组件串联起来。BusFactory负责Bus的创建。默认的BusFactory是SpringBusFactory,对应于默认* 的Bus实现。在构造过程中,SpringBusFactory会搜索META-INF/cxf(包含在 CXF 的jar中)下的所有bean配置文件。* 根据这些配置文件构建一个ApplicationContext。开发者也可以提供自己的配置文件来定制Bus。*/@Bean(name = Bus.DEFAULT_BUS_ID)public SpringBus springBus() {return new SpringBus();}/*** 设置WebService访问父路径* <p>* 此方法作用是改变项目中服务名的前缀名,此处127.0.0.1或者localhost不能访问时,请使用ipconfig查看本机ip来访问* 此方法被注释后, 即不改变前缀名(默认是services), wsdl访问地址为 http://127.0.0.1:8080/services/ws/api?wsdl* 去掉注释后wsdl访问地址为:http://127.0.0.1:8080/webServices/ws/api?wsdl* http://127.0.0.1:8080/soap/列出服务列表 或 http://127.0.0.1:8080/soap/ws/api?wsdl 查看实际的服务* 新建Servlet记得需要在启动类添加注解:@ServletComponentScan* 如果启动时出现错误:not loaded because DispatcherServlet Registration found non dispatcher servlet dispatcherServlet* 可能是springboot与cfx版本不兼容。* 同时在spring boot2.0.6之后的版本与xcf集成,不需要在定义以下方法,直接在application.properties配置文件中添加:* cxf.path=/service(默认是services)*/@Beanpublic ServletRegistrationBean getRegistrationBean() {return new ServletRegistrationBean(new CXFServlet(), "/webServices/*");}@Beanpublic Endpoint messageEndPoint() {EndpointImpl endpoint = new EndpointImpl(springBus(), this.smsService);endpoint.publish("/smsService");
//        endpoint.getInInterceptors().add(this.interceptor);return endpoint;}/*用于发布多个WebService*/
//    @Bean
//    public Endpoint messageEndPoint2() {
//        EndpointImpl endpoint = new EndpointImpl(springBus(), this.ageInfoService);
//        endpoint.publish("/smsService");
//        endpoint.getInInterceptors().add(this.interceptor);
//        return endpoint;
//    }
}

3.service

package com.ruoyi.webservice;import com.ruoyi.dto.SmsRequest;
import com.ruoyi.dto.SmsResponse;import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;@WebService(name = "smsService", targetNamespace = "http://tempuri.org")
public interface SmsService {@WebMethod(action = "http://tempuri.org/sendSms" ,operationName = "sendSms")SmsResponse sendSms(@WebParam(name = "sysname") String sysname,@WebParam(name = "mobiles") String mobiles,@WebParam(name = "content") String content);
}

4.serviceimpl

package com.ruoyi.webservice.impl;import com.ruoyi.dto.SmsResponse;
import com.ruoyi.webservice.SmsService;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;import javax.jws.WebParam;
import javax.jws.WebService;@Service
@WebService(name = "smsService", targetNamespace = "http://tempuri.org")
public class SmsServiceImpl implements SmsService {@Overridepublic SmsResponse sendSms(@WebParam(name = "sysname") String sysname,@WebParam(name = "mobiles") String mobiles,@WebParam(name = "content") String content) {// 实现发送短信的逻辑SmsResponse response = new SmsResponse();// 设置响应状态码和消息response.setCode(HttpStatus.OK.value());response.setMessage("短信发送成功");return response;}
}

5.测试类

package com.ruoyi.test;import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONUtil;public class SmsServiceTest {private static final String ADDRESS = "http://localhost:8080/webServices/smsService";private static final String SOAP_ACTION = "sendSms";private static final String NAMESPACE = "http://tempuri.org";public static void main(String[] args) {try {// 创建HttpRequest对象HttpRequest request = HttpRequest.post(ADDRESS);// 构造SOAP请求体String soapBody = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +"<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" " +"xmlns:sns=\"" + NAMESPACE + "\">" + // 使用服务的命名空间"<soap:Body>" +"<sns:" + SOAP_ACTION + ">" +"<sysname>您的系统名称</sysname>" +"<mobiles>13800000000</mobiles>" +"<content>这是一条测试短信</content>" +"</sns:" + SOAP_ACTION + ">" +"</soap:Body>" +"</soap:Envelope>";// 设置请求头信息request.header("Content-Type", "text/xml; charset=utf-8").header("SOAPAction", NAMESPACE + "/" + SOAP_ACTION);// 设置请求体request.body(soapBody);// 发送请求并获取响应HttpResponse response = request.execute();// 打印响应内容String responseContent = response.body();System.out.println(responseContent);// 将响应内容转换为JSON(如果需要)String jsonResponse = JSONUtil.parse(responseContent).toString();System.out.println(jsonResponse);} catch (Exception e) {e.printStackTrace();}}
}

这篇关于spring boot 集成webservice soap发短信【干货踩坑、亲测可用、干就完了】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1118666

相关文章

SpringBoot整合liteflow的详细过程

《SpringBoot整合liteflow的详细过程》:本文主要介绍SpringBoot整合liteflow的详细过程,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋...  liteflow 是什么? 能做什么?总之一句话:能帮你规范写代码逻辑 ,编排并解耦业务逻辑,代码

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

Spring Security中用户名和密码的验证完整流程

《SpringSecurity中用户名和密码的验证完整流程》本文给大家介绍SpringSecurity中用户名和密码的验证完整流程,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定... 首先创建了一个UsernamePasswordAuthenticationTChina编程oken对象,这是S

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

Java easyExcel实现导入多sheet的Excel

《JavaeasyExcel实现导入多sheet的Excel》这篇文章主要为大家详细介绍了如何使用JavaeasyExcel实现导入多sheet的Excel,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录1.官网2.Excel样式3.代码1.官网easyExcel官网2.Excel样式3.代码

Java MQTT实战应用

《JavaMQTT实战应用》本文详解MQTT协议,涵盖其发布/订阅机制、低功耗高效特性、三种服务质量等级(QoS0/1/2),以及客户端、代理、主题的核心概念,最后提供Linux部署教程、Sprin... 目录一、MQTT协议二、MQTT优点三、三种服务质量等级四、客户端、代理、主题1. 客户端(Clien

Java中调用数据库存储过程的示例代码

《Java中调用数据库存储过程的示例代码》本文介绍Java通过JDBC调用数据库存储过程的方法,涵盖参数类型、执行步骤及数据库差异,需注意异常处理与资源管理,以优化性能并实现复杂业务逻辑,感兴趣的朋友... 目录一、存储过程概述二、Java调用存储过程的基本javascript步骤三、Java调用存储过程示

Spring 框架之Springfox使用详解

《Spring框架之Springfox使用详解》Springfox是Spring框架的API文档工具,集成Swagger规范,自动生成文档并支持多语言/版本,模块化设计便于扩展,但存在版本兼容性、性... 目录核心功能工作原理模块化设计使用示例注意事项优缺点优点缺点总结适用场景建议总结Springfox 是

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试