0

0

Spring Boot 中接收 SOAP 消息并转换为 JSON 的教程

心靈之曲

心靈之曲

发布时间:2025-09-26 21:25:11

|

418人浏览过

|

来源于php中文网

原创

spring boot 中接收 soap 消息并转换为 json 的教程

本文将指导你如何在 Spring Boot 应用中接收 SOAP 请求,将其转换为 JSON 格式,并调用 REST API。同时,还将介绍如何接收 REST API 的 JSON 响应,并将其转换回 SOAP 响应。通过 Spring Web Services 和 Spring MVC 的强大功能,可以简化 SOAP 和 REST 通信之间的桥接过程,无需手动进行复杂的转换。

搭建 Spring Boot 项目

首先,你需要创建一个 Spring Boot 项目。可以使用 Spring Initializr (https://www.php.cn/link/4ac20f72e05b86b3dc759608b60f5d67) 来快速生成项目骨架。 在 Spring Initializr 中,选择以下依赖:

  • Spring Web Services
  • Spring Web
  • JAXB (如果你的 SOAP 定义使用了 JAXB)

定义 SOAP 端点

使用 @Endpoint 注解创建一个类,用于处理 SOAP 请求。@PayloadRoot 注解用于指定处理特定 SOAP 消息的方法。

import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
import org.springframework.web.client.RestTemplate;

@Endpoint
public class YourEndpoint {

  private final RestTemplate restTemplate;

  private static final String REST_URL = "your_rest_api_url"; // Replace with your REST API URL

  public YourEndpoint(RestTemplate restTemplate) {
    this.restTemplate = restTemplate;
  }

  @PayloadRoot(localPart = "YourRequestFromWsdlRequest", namespace = "your_wsdl_namespace")
  @ResponsePayload
  public YourResponseFromWsdl method(@RequestPayload YourRequestFromWsdl request) {

    // Call REST API with the request
    YourResponseFromWsdl response = restTemplate.postForObject(REST_URL, request, YourResponseFromWsdl.class);

    return response;
  }
}

代码解释:

  • @Endpoint: 声明该类为一个 SOAP 端点。
  • @PayloadRoot: 指定处理的 SOAP 消息的本地名称和命名空间。 localPart 对应于 WSDL 中定义的请求消息的名称,namespace 对应于 WSDL 中定义的命名空间。 请确保将 your_wsdl_namespace 替换为 WSDL 中定义的实际命名空间。
  • @RequestPayload: 将 SOAP 请求的消息体绑定到 YourRequestFromWsdl 对象。
  • @ResponsePayload: 将方法的返回值作为 SOAP 响应的消息体返回。
  • RestTemplate: 用于调用 REST API。你需要注入一个 RestTemplate 实例。
  • REST_URL: 替换为你的 REST API 的 URL。

依赖注入 RestTemplate:

你需要配置 RestTemplate bean。 在你的 Spring Boot 应用配置类中添加以下代码:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class AppConfig {

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

定义 JAXB 对象

你需要根据你的 WSDL 文件生成对应的 Java 对象。 可以使用 xjc 工具来完成这个任务。 例如:

xjc your_wsdl_file.wsdl -d src/main/java

这将生成与 WSDL 文件对应的 Java 类,包括 YourRequestFromWsdl 和 YourResponseFromWsdl。

确保命名空间正确:

Face++旷视
Face++旷视

Face⁺⁺ AI开放平台

下载

在生成的 JAXB 对象中,确保 @XmlType 和 @XmlRootElement 注解的 namespace 属性与你的 WSDL 文件中的命名空间一致。

使用 WebClient (可选)

除了 RestTemplate,你还可以使用 WebClient 来调用 REST API。 WebClient 是一个非阻塞的、响应式的 HTTP 客户端,可以提供更好的性能和可伸缩性。

import org.springframework.web.reactive.function.client.WebClient;

@Endpoint
public class YourEndpoint {

  private final WebClient webClient;

  private static final String REST_URL = "your_rest_api_url";

  public YourEndpoint(WebClient webClient) {
    this.webClient = webClient;
  }

  @PayloadRoot(localPart = "YourRequestFromWsdlRequest", namespace = "your_wsdl_namespace")
  @ResponsePayload
  public YourResponseFromWsdl method(@RequestPayload YourRequestFromWsdl request) {

    YourResponseFromWsdl response = webClient.post()
        .uri(REST_URL)
        .bodyValue(request)
        .retrieve()
        .bodyToMono(YourResponseFromWsdl.class)
        .block(); // Use block() for synchronous execution. Consider asynchronous handling in production.

    return response;
  }
}

配置 WebClient:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;

@Configuration
public class AppConfig {

    @Bean
    public WebClient webClient() {
        return WebClient.create();
    }
}

配置 Spring Web Services

在你的 Spring Boot 应用中,你需要配置 Spring Web Services 来处理 SOAP 请求。 创建一个配置类,并添加以下代码:

import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.ws.config.annotation.EnableWs;
import org.springframework.ws.transport.http.MessageDispatcherServlet;
import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
import org.springframework.xml.xsd.SimpleXsdSchema;
import org.springframework.xml.xsd.XsdSchema;

@EnableWs
@Configuration
public class WebServiceConfig {

    @Bean
    public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean<>(servlet, "/ws/*"); // Map to /ws/*
    }

    @Bean(name = "yourWsdlName") // Replace with your WSDL name
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema yourSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("YourPort"); // Replace with your PortType name
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("your_wsdl_namespace"); // Replace with your WSDL namespace
        wsdl11Definition.setSchema(yourSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema yourSchema() {
        return new SimpleXsdSchema(new ClassPathResource("your_schema.xsd")); // Replace with your schema file
    }
}

代码解释:

  • @EnableWs: 启用 Spring Web Services。
  • MessageDispatcherServlet: Spring Web Services 的核心 Servlet,用于处理 SOAP 请求。
  • DefaultWsdl11Definition: 用于生成 WSDL 文件。
  • XsdSchema: 用于定义 WSDL 文件中使用的 XML Schema。

配置要点:

  • 将 yourWsdlName 替换为你的 WSDL 文件的名称。
  • 将 YourPort 替换为你的 WSDL 文件中定义的 portType 的名称。
  • 将 your_wsdl_namespace 替换为你的 WSDL 文件中定义的命名空间。
  • 将 your_schema.xsd 替换为你的 XML Schema 文件的路径。 你需要根据你的 WSDL 文件创建一个 XSD 文件。

异常处理

在实际应用中,需要处理可能发生的异常,例如 REST API 调用失败或数据转换错误。 可以使用 @ExceptionHandler 注解来处理这些异常。

注意事项

  • 确保你的 WSDL 文件是有效的,并且生成的 Java 对象与 WSDL 文件中的定义一致。
  • 在生产环境中,应该使用异步方式处理 REST API 调用,以避免阻塞 SOAP 请求的处理线程。
  • 考虑添加日志记录和监控,以便更好地了解你的服务的运行状况。
  • 确保你的 Spring Boot 应用已正确配置,并且所有必要的依赖项都已添加到项目中。
  • 仔细检查命名空间和本地名称,确保它们与 WSDL 文件中的定义完全匹配。
  • 对于 WebClient,在生产环境中使用 block() 可能会导致性能问题。 考虑使用异步方式处理响应。

通过以上步骤,你就可以在 Spring Boot 应用中成功地接收 SOAP 消息,将其转换为 JSON 格式,并调用 REST API。 Spring Web Services 和 Spring MVC 提供了强大的功能,可以简化 SOAP 和 REST 通信之间的桥接过程。

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

WorkBuddy
WorkBuddy

腾讯云推出的AI原生桌面智能体工作台

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
spring框架介绍
spring框架介绍

本专题整合了spring框架相关内容,想了解更多详细内容,请阅读专题下面的文章。

161

2025.08.06

Java Spring Security 与认证授权
Java Spring Security 与认证授权

本专题系统讲解 Java Spring Security 框架在认证与授权中的应用,涵盖用户身份验证、权限控制、JWT与OAuth2实现、跨站请求伪造(CSRF)防护、会话管理与安全漏洞防范。通过实际项目案例,帮助学习者掌握如何 使用 Spring Security 实现高安全性认证与授权机制,提升 Web 应用的安全性与用户数据保护。

89

2026.01.26

spring boot框架优点
spring boot框架优点

spring boot框架的优点有简化配置、快速开发、内嵌服务器、微服务支持、自动化测试和生态系统支持。本专题为大家提供spring boot相关的文章、下载、课程内容,供大家免费下载体验。

139

2023.09.05

spring框架有哪些
spring框架有哪些

spring框架有Spring Core、Spring MVC、Spring Data、Spring Security、Spring AOP和Spring Boot。详细介绍:1、Spring Core,通过将对象的创建和依赖关系的管理交给容器来实现,从而降低了组件之间的耦合度;2、Spring MVC,提供基于模型-视图-控制器的架构,用于开发灵活和可扩展的Web应用程序等。

410

2023.10.12

Java Spring Boot开发
Java Spring Boot开发

本专题围绕 Java 主流开发框架 Spring Boot 展开,系统讲解依赖注入、配置管理、数据访问、RESTful API、微服务架构与安全认证等核心知识,并通过电商平台、博客系统与企业管理系统等项目实战,帮助学员掌握使用 Spring Boot 快速开发高效、稳定的企业级应用。

73

2025.08.19

Java Spring Boot 4更新教程_Java Spring Boot 4有哪些新特性
Java Spring Boot 4更新教程_Java Spring Boot 4有哪些新特性

Spring Boot 是一个基于 Spring 框架的 Java 开发框架,它通过 约定优于配置的原则,大幅简化了 Spring 应用的初始搭建、配置和开发过程,让开发者可以快速构建独立的、生产级别的 Spring 应用,无需繁琐的样板配置,通常集成嵌入式服务器(如 Tomcat),提供“开箱即用”的体验,是构建微服务和 Web 应用的流行工具。

153

2025.12.22

Java Spring Boot 微服务实战
Java Spring Boot 微服务实战

本专题深入讲解 Java Spring Boot 在微服务架构中的应用,内容涵盖服务注册与发现、REST API开发、配置中心、负载均衡、熔断与限流、日志与监控。通过实际项目案例(如电商订单系统),帮助开发者掌握 从单体应用迁移到高可用微服务系统的完整流程与实战能力。

271

2025.12.24

Spring Boot企业级开发与MyBatis Plus实战
Spring Boot企业级开发与MyBatis Plus实战

本专题面向 Java 后端开发者,系统讲解如何基于 Spring Boot 与 MyBatis Plus 构建高效、规范的企业级应用。内容涵盖项目架构设计、数据访问层封装、通用 CRUD 实现、分页与条件查询、代码生成器以及常见性能优化方案。通过完整实战案例,帮助开发者提升后端开发效率,减少重复代码,快速交付稳定可维护的业务系统。

35

2026.02.11

TypeScript类型系统进阶与大型前端项目实践
TypeScript类型系统进阶与大型前端项目实践

本专题围绕 TypeScript 在大型前端项目中的应用展开,深入讲解类型系统设计与工程化开发方法。内容包括泛型与高级类型、类型推断机制、声明文件编写、模块化结构设计以及代码规范管理。通过真实项目案例分析,帮助开发者构建类型安全、结构清晰、易维护的前端工程体系,提高团队协作效率与代码质量。

69

2026.03.13

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
React 教程
React 教程

共58课时 | 6.1万人学习

国外Web开发全栈课程全集
国外Web开发全栈课程全集

共12课时 | 1万人学习

React核心原理新老生命周期精讲
React核心原理新老生命周期精讲

共12课时 | 1.1万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号