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

相关文章

Java实现检查多个时间段是否有重合

《Java实现检查多个时间段是否有重合》这篇文章主要为大家详细介绍了如何使用Java实现检查多个时间段是否有重合,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录流程概述步骤详解China编程步骤1:定义时间段类步骤2:添加时间段步骤3:检查时间段是否有重合步骤4:输出结果示例代码结语作

Java中String字符串使用避坑指南

《Java中String字符串使用避坑指南》Java中的String字符串是我们日常编程中用得最多的类之一,看似简单的String使用,却隐藏着不少“坑”,如果不注意,可能会导致性能问题、意外的错误容... 目录8个避坑点如下:1. 字符串的不可变性:每次修改都创建新对象2. 使用 == 比较字符串,陷阱满

Java判断多个时间段是否重合的方法小结

《Java判断多个时间段是否重合的方法小结》这篇文章主要为大家详细介绍了Java中判断多个时间段是否重合的方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录判断多个时间段是否有间隔判断时间段集合是否与某时间段重合判断多个时间段是否有间隔实体类内容public class D

IDEA编译报错“java: 常量字符串过长”的原因及解决方法

《IDEA编译报错“java:常量字符串过长”的原因及解决方法》今天在开发过程中,由于尝试将一个文件的Base64字符串设置为常量,结果导致IDEA编译的时候出现了如下报错java:常量字符串过长,... 目录一、问题描述二、问题原因2.1 理论角度2.2 源码角度三、解决方案解决方案①:StringBui

Java覆盖第三方jar包中的某一个类的实现方法

《Java覆盖第三方jar包中的某一个类的实现方法》在我们日常的开发中,经常需要使用第三方的jar包,有时候我们会发现第三方的jar包中的某一个类有问题,或者我们需要定制化修改其中的逻辑,那么应该如何... 目录一、需求描述二、示例描述三、操作步骤四、验证结果五、实现原理一、需求描述需求描述如下:需要在

Debezium 与 Apache Kafka 的集成方式步骤详解

《Debezium与ApacheKafka的集成方式步骤详解》本文详细介绍了如何将Debezium与ApacheKafka集成,包括集成概述、步骤、注意事项等,通过KafkaConnect,D... 目录一、集成概述二、集成步骤1. 准备 Kafka 环境2. 配置 Kafka Connect3. 安装 D

Java中ArrayList和LinkedList有什么区别举例详解

《Java中ArrayList和LinkedList有什么区别举例详解》:本文主要介绍Java中ArrayList和LinkedList区别的相关资料,包括数据结构特性、核心操作性能、内存与GC影... 目录一、底层数据结构二、核心操作性能对比三、内存与 GC 影响四、扩容机制五、线程安全与并发方案六、工程

JavaScript中的reduce方法执行过程、使用场景及进阶用法

《JavaScript中的reduce方法执行过程、使用场景及进阶用法》:本文主要介绍JavaScript中的reduce方法执行过程、使用场景及进阶用法的相关资料,reduce是JavaScri... 目录1. 什么是reduce2. reduce语法2.1 语法2.2 参数说明3. reduce执行过程

如何使用Java实现请求deepseek

《如何使用Java实现请求deepseek》这篇文章主要为大家详细介绍了如何使用Java实现请求deepseek功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1.deepseek的api创建2.Java实现请求deepseek2.1 pom文件2.2 json转化文件2.2

Java调用DeepSeek API的最佳实践及详细代码示例

《Java调用DeepSeekAPI的最佳实践及详细代码示例》:本文主要介绍如何使用Java调用DeepSeekAPI,包括获取API密钥、添加HTTP客户端依赖、创建HTTP请求、处理响应、... 目录1. 获取API密钥2. 添加HTTP客户端依赖3. 创建HTTP请求4. 处理响应5. 错误处理6.