在 Spring 中将 SOAP 1.2 与 WebServiceGatewaySupport 结合使用

2024-02-15

我对 Spring 框架非常陌生,在使用 Spring 创建一个简单的 SOAP 客户端时遇到了一些问题。

像一个好的新手一样,我使用 Spring 教程来制作我的 SOAP 客户端。你可以在这里找到它 ->https://spring.io/guides/gs/consuming-web-service/ https://spring.io/guides/gs/consuming-web-service/

使用示例中给出的Web服务进行测试是可以的。我已经更改了代码以使其与我的网络服务一起使用,所以这是文件:

SetCodePinConfigurartion.java

package hello;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

@Configuration
public class SetPinCodeConfiguration {

@Bean
public Jaxb2Marshaller marshaller() {
    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    // this package must match the package in the <generatePackage> specified in
    // pom.xml
    marshaller.setContextPath("hello.wsdl");
    return marshaller;
}

@Bean
public SetPinCodeClient SetPinCodeClient(Jaxb2Marshaller marshaller) {
    SetPinCodeClient client = new SetPinCodeClient();
    client.setDefaultUri("http://xxx.xxx.xx.x/PlayerServices/PlayerIntelligence/PlayerManagementService");
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);
    return client;
}
}

SetPinCodeClient.java

package hello;

import javax.xml.bind.JAXBElement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;

import hello.wsdl.SetPinCode;
import hello.wsdl.SetPinCodeResponse;

public class SetPinCodeClient extends WebServiceGatewaySupport{

private static final Logger log = LoggerFactory.getLogger(SetPinCodeClient.class);

public SetPinCodeResponse SetPinCode(JAXBElement<String> pinCode, JAXBElement<String> cardNumber, JAXBElement<Integer> casinoCreationId) {

    SetPinCode request = new SetPinCode();
    request.setPinCode(pinCode);
    request.setCardNumber(cardNumber);
    request.setCasinoCreationId(casinoCreationId);

    log.info("Requesting save in database for card " + cardNumber + "with pin code" + pinCode + "from casino n°" + casinoCreationId);

    SetPinCodeResponse response = (SetPinCodeResponse) getWebServiceTemplate()
            .marshalSendAndReceive("http://xxx.xxx.xx.x/PlayerServices/PlayerIntelligence/PlayerManagementService",
                    request,
                    new SoapActionCallback("http://xxx.xxx.xx.x/PlayerServices/PlayerIntelligence/PlayerManagementService/SetPinCode"));

    return response;
}
}

应用程序.java

package hello;

import javax.xml.bind.JAXBElement;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import hello.wsdl.ObjectFactory;
import hello.wsdl.SetPinCodeResponse;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Bean
CommandLineRunner lookup(SetPinCodeClient setPinCodeClient) {
    return args -> {
        JAXBElement<String> pinCode = new ObjectFactory().createSetPinCodeCardNumber("2004");
        JAXBElement<String> cardNumber = new ObjectFactory().createSetPinCodeCardNumber("B09B036");
        JAXBElement<Integer> casinoCreationId = new ObjectFactory().createSetPinCodeCasinoCreationId(185);

        SetPinCodeResponse response = setPinCodeClient.SetPinCode(pinCode, cardNumber, casinoCreationId);
        System.err.println(response.toString());
    };
}
}

但当我启动应用程序时出现错误

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:793) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:774) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at hello.Application.main(Application.java:18) [classes/:na]
Caused by: org.springframework.ws.client.WebServiceTransportException: Cannot process the message because the content type 'text/xml; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8'. [415]
at org.springframework.ws.client.core.WebServiceTemplate.handleError(WebServiceTemplate.java:699) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:609) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:555) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at hello.SetPinCodeClient.SetPinCode(SetPinCodeClient.java:33) ~[classes/:na]
at hello.Application.lambda$0(Application.java:28) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:790) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
... 5 common frames omitted

所以看来我需要更改 SOAP 1.2 的协议,但我找不到正确的方法。我已经测试过这个解决方案SetPinCodeClient.java

public SetPinCodeResponse SetPinCode(JAXBElement<String> pinCode, JAXBElement<String> cardNumber, JAXBElement<Integer> casinoCreationId) {

    SetPinCode request = new SetPinCode();
    request.setPinCode(pinCode);
    request.setCardNumber(cardNumber);
    request.setCasinoCreationId(casinoCreationId);

    log.info("Requesting save in database for card " + cardNumber + "with pin code" + pinCode + "from casino n°" + casinoCreationId);

    SaajSoapMessageFactory messageFactory = new SaajSoapMessageFactory();
    messageFactory.setSoapVersion(SoapVersion.SOAP_12);
    WebServiceTemplate wsTemplate = getWebServiceTemplate();
    wsTemplate.setMessageFactory(messageFactory);

    SetPinCodeResponse response = (SetPinCodeResponse) wsTemplate
            .marshalSendAndReceive("http://192.168.67.63:8095/PlayerServices/PlayerIntelligence/PlayerManagementService",
                    request,
                    new SoapActionCallback("http://192.168.67.63:8095/PlayerServices/PlayerIntelligence/PlayerManagementService/SetPinCode"));

    return response;
}

但如果我这样做,它会将协议更改为 SOAP 1.2,但之后我会遇到此错误

java.lang.IllegalStateException: Failed to execute CommandLineRunner
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:793) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:774) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1234) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
at hello.Application.main(Application.java:18) [classes/:na]
Caused by: java.lang.NullPointerException: null
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:174) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.soap.saaj.SaajSoapMessageFactory.createWebServiceMessage(SaajSoapMessageFactory.java:60) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.context.DefaultMessageContext.<init>(DefaultMessageContext.java:42) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:553) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:390) ~[spring-ws-core-3.0.0.RELEASE.jar:na]
at hello.SetPinCodeClient.SetPinCode(SetPinCodeClient.java:36) ~[classes/:na]
at hello.Application.lambda$0(Application.java:28) [classes/:na]
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:790) [spring-boot-2.0.0.RELEASE.jar:2.0.0.RELEASE]
... 5 common frames omitted

一个 NullPointerException 因为我的对象 messageFactory 是 NULL....

所以问题是,如何才能最好地更改为 SOAP 1.2? 感谢您的回答。


所以我终于找到了一种使用 SOAP 1.2 协议进行调用的方法。

这是完整的代码SetPinCodeClient.java

package hello;

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ws.WebServiceMessage;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.SoapHeader;
import org.springframework.ws.soap.SoapHeaderElement;
import org.springframework.ws.soap.client.core.SoapActionCallback;
import org.springframework.ws.soap.saaj.SaajSoapMessage;
import org.springframework.ws.soap.saaj.SaajSoapMessageFactory;

import hello.wsdl.SetPinCode;
import hello.wsdl.SetPinCodeResponse;

public class SetPinCodeClient extends WebServiceGatewaySupport{

private static final Logger log = LoggerFactory.getLogger(SetPinCodeClient.class);

public SetPinCodeResponse SetPinCode(JAXBElement<String> pinCode, JAXBElement<String> cardNumber, JAXBElement<Integer> casinoCreationId) throws SOAPException {
    String action ="http://tempuri.org/Player_x0020_Management_x0020_Service/SetPinCode";
    String uri = "http://xxx.xxx.xx.x/PlayerServices/PlayerIntelligence/PlayerManagementService";

    SetPinCode request = new SetPinCode();
    request.setPinCode(pinCode);
    request.setCardNumber(cardNumber);
    request.setCasinoCreationId(casinoCreationId);

    log.info("Requesting save in database for card " + cardNumber + "with pin code" + pinCode + "from casino n°" + casinoCreationId);

    MessageFactory msgFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
    SaajSoapMessageFactory saajSoapMessageFactory = new SaajSoapMessageFactory(msgFactory);
    WebServiceTemplate wsTemplate = getWebServiceTemplate();
    wsTemplate.setMessageFactory(saajSoapMessageFactory);

    SoapActionCallback requestCallback = new SoapActionCallback(action) {
        public void doWithMessage(WebServiceMessage message) {
            SaajSoapMessage soapMessage = (SaajSoapMessage) message;
            SoapHeader soapHeader = soapMessage.getSoapHeader();

            QName wsaToQName = new QName("http://www.w3.org/2005/08/addressing", "To", "wsa");
            SoapHeaderElement wsaTo =  soapHeader.addHeaderElement(wsaToQName);
            wsaTo.setText(uri);

            QName wsaActionQName = new QName("http://www.w3.org/2005/08/addressing", "Action", "wsa");
            SoapHeaderElement wsaAction =  soapHeader.addHeaderElement(wsaActionQName);
            wsaAction.setText(action);
        }
    };

    SetPinCodeResponse response = (SetPinCodeResponse) wsTemplate.marshalSendAndReceive(uri, request, requestCallback);

    return response;


}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Spring 中将 SOAP 1.2 与 WebServiceGatewaySupport 结合使用 的相关文章

  • 我在使用 JavaFX 绘制十字时遇到问题

    我正在尝试编写代码 在网格上对角绘制 3 个形状 前两个形状是正方形和圆形 我能做到 然而 第三种形状让我有些悲伤 我应该画一个十字 T 版本 而不是 X 每次我写出代码时 它看起来就像一个侧面 我知道我只是错过了一些简单的东西 但我真的很
  • 从另一个进程捕获 system.out 消息

    我有一个 JVM 1 它启动 JVM 2 我希望能够在 JVM 1 中监视来自 JVM 2 的 System out println 调用 直接的方法是 JVM A 执行系统命令来启动 JVM B 然后 JVM A 读取 B 的所有输出 S
  • Grails 项目 - Servlet 调用 - ClassNotFoundException:javax.servlet.AsyncContext

    我在用 IntelliJ IDEA 终极版 12 4 grails 2 2 0 BuildConfig groovy 文件中的 grails servlet version 2 5 并实现了简单的 servlet post 请求 使用 RE
  • 模式更新后 jOOQ 生成的类的运行时验证?

    我用org jooq util DefaultGenerator在构建过程中生成 jOOQ 类来表示我的数据库模式 当应用程序运行时 架构预计会在应用程序不知情的情况下发生更改 此类更改可能与已生成的代码兼容 也可能不兼容 如何在运行时检测
  • 在 json 中解析尾随字符

    我正在尝试检查 json 是否有效 并且我遇到了奇怪的行为 当我将一些字符附加到可解析的 json 时 jackson 和 gson 都会解析它 并且它们会忽略尾随字符 我想检查 json 是否严格有效 请帮忙 我尝试了几个标志mapper
  • Android:TelephonyManager 类

    我不明白为什么 API 文档中这么写TelephonyManager类是public 但是当我尝试创建一个实例时 它说它不是公共类 并且无法从包中访问 我看到它也说使用Context getSystemService Context TEL
  • JTree ConvertValueToText 返回在更改时被截断

    我有一个自定义树实现convertValueToText 此实现取决于某些全局状态 如果返回的字符串比先前返回的字符串更长 实际上我认为更宽 因为以像素为单位触发它 则文本将被截断并用 填充 当重绘是由 取消 选择元素或某个元素引起时 情况
  • 如何在 QueryDSL 中选择文字

    我目前正在开发一个使用 queryDSL 和 hibernate 的项目 其中它需要一个选择文字 按照发布的示例here https stackoverflow com questions 18691317 querydsl how to
  • Java ArrayList 和 HashMap 动态

    有人可以提供一个创建Java的例子吗ArrayList and HashMap在飞行中 所以而不是做一个add or put 实际上在类实例化时为数组 哈希提供种子数据 举个例子 类似于 PHP 的例子 array array 3 1 2
  • 不使用 length() 方法的字符串长度[关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 如何在不使用字符串的情况下找到字符串的长度length String类的方法 str toCharArray length应该管用 或者怎么
  • 将 Maven 控制台与 m2eclipse 一起使用

    Maven 新手在这里 有没有办法在 Eclipse 中打开控制台并在 M2Eclipse 插件上执行 Maven 命令 这是一个非常好的插件 但我环顾四周 没有找到我想要的一些功能 谢谢 如果你想运行特定的maven插件 你可以这样做 g
  • 为什么replaceAll在这行代码中不起作用? [复制]

    这个问题在这里已经有答案了 String weatherLocation weatherLoc 1 toString weatherLocation replaceAll how weatherLocation replaceAll wea
  • “强制更新快照/版本” - 这是什么意思

    在 Maven 项目中 选择 更新项目 时 有一个名为 强制更新快照 版本 的选项 它有什么作用 强制更新快照 版本 就像运行以下命令 mvn U install U 也可以用作 update snapshot 看here http boo
  • Web 服务客户端的 AXIS 与 JAX-WS

    我决定用Java 实现Web 服务客户端 我已经在 Eclipse 中生成了 Axis 客户端 并使用 wsimport 生成了 JAS WS 客户端 两种解决方案都有效 现在我必须选择一种来继续 在选择其中之一之前我应该 考虑什么 JAX
  • 按钮悬停和按下效果 CSS Javafx

    我是 CSS 新手 为按钮定义了以下 CSS 样式 其中id并且应用了自定义样式 但不应用悬停和按下效果 bevel grey fx background color linear gradient f2f2f2 d6d6d6 linear
  • 对于双核手机,availableProcessors() 返回 1

    我最近购买了一部 Moto Atrix 2 手机 当我尝试查看手机中的处理器规格时 Runtime getRuntime availableProcessors 返回 1 proc cpuinfo 也仅包含有关处理器 0 的信息 出于好奇
  • 背景图像隐藏其他组件,例如按钮标签等,反之亦然

    如何解决此代码中组件的隐藏问题 代码运行没有错误 但背景图片不显示 如何更改代码以获取背景图像 使用验证方法时 它在validation 中创建错误 public class TEST public TEST String strm Jan
  • java中什么是静态接口?

    我正在阅读Map Entry界面 当我注意到它是一个static界面 我不太明白什么是静态接口 它与常规接口有什么不同 public static interface Map Entry
  • 需要在没有wsdl的情况下调用soap ws

    我是网络服务的新手 这个网络服务是由 siebel 提供的 我需要调用一项网络服务 我的客户向我提供了以下详细信息 这是 SOAP 对于产品 请使用它作为端点 Request
  • removeall 和removeif 的用例

    我找到了这个 fun main val list MutableList

随机推荐

  • ZeroMQ 多线程:按需创建套接字还是使用套接字对象池?

    我正在利用 ZeroMQ N to N 发布 订阅模型构建一个 POC 在我们的应用服务器中 当处理 http 请求时 如果线程从数据库中提取数据 它就会使用该数据更新本地 memcache 实例 为了同步应用程序服务器集群中的其他 mem
  • Haskell N 叉树遍历

    我对 Haskell 还很陌生 我正在尝试找出如何遍历 n 叉树 作为输出 我希望获得叶值列表 因为分支没有值 因此对于 testtree 这将是 4 5 到目前为止我的定义是 data Tree a Leaf a Branch Tree
  • 替换 Fragment 时出现 IllegalStateException

    它是使用兼容性包的小型 Android 2 2 测试应用程序 这是我在收到点击时尝试替换片段的 当然是错误的 方法 我试图用同一 Fragment 类的新 不同 实例替换它 正如我将解释的那样 它无法按预期工作 我需要帮助 public c
  • 两个类里面的代码几乎重复

    此时此刻 我有两节课 UserHibernateDao and TicketHibernateDao import java util List import org springframework orm hibernate3 suppo
  • 如何转义 SQLite FTS 查询的字符串

    我正在尝试使用不受信任的用户输入执行 SQLite FTS 查询 我不想让用户访问查询语法 也就是说他们将无法执行类似的匹配查询foo OR bar AND cats 如果他们尝试使用该字符串进行查询 我想将其解释为更像是foo OR ba
  • Android 网络提供商,需要互联网吗?

    我正在使用网络提供商进行位置更新 我的手机需要联网吗 这是我的代码 LocationMngr LocationManager getSystemService Context LOCATION SERVICE LocationMngr re
  • 使用 pojo 自动完成 primefaces

    我读过一些关于同一组件的质量检查 但我觉得我错过了一些东西 因为我落后了一步 当我使用 primefaces 自动完成组件时 我什至无法打开页面 它的片段是
  • JavaScript 中的 Splat 运算符相当于 Python 中的 *args 和 **kwargs?

    我经常使用 Python 而且我现在正在快速学习 JavaScript 或者我应该说重新学习 所以我想问一下 相当于什么 args and kwargs在 JavaScript 中 ES6 在 JavaScript 中添加了扩展运算符 fu
  • 如何从 CMake 命令行更改 C++ 标准?

    目前我有一个需要 C 17 的项目 因此在CMakeLists txt我很早就有这条线 set CMAKE CXX STANDARD 17 从命令行 cmake 偶尔我想测试该项目是否也可以使用 C 20 进行编译 以避免意外 如何选择从命
  • 我的应用或其依赖项是否违反了 Android 广告 ID 政策?

    我刚刚收到来自 Google Play 的这条消息 但我没有收集广告 ID 警告原因 违反Android广告ID使用 政策和开发者分发协议第 4 8 条 Google Play 要求开发者在以下情况下提供有效的隐私政策 应用程序请求或处理敏
  • 如何在运行时修改 web.config appSettings?

    我对如何在运行时修改 web config appSettings 值感到困惑 例如 我有这个 appSettings 部分
  • 为什么 Haskell 不能正确排序这些 IO 操作?

    我的一个朋友问我为什么要学习 Haskell 为了展示 Haskell 的强大功能 我编写了一个小程序来显示素数列表 main do putStr Enter the number of prime numbers to display n
  • 在 VB.NET 表单之间传递数据

    我有一个带有按钮的表单 单击时会弹出一个对话框表单 在此对话框中 用户需要选择一些数据 完成后单击 确定 按钮 一旦他们单击 确定 按钮 它需要将一个整数返回到之前的形式 我创建了一个对话框表单并尝试通过以下代码调用它 Dim intRes
  • JsTree 与 jquery.validate 冲突

    我有一个 Jstree 填充项目列表 当我单击一个节点时 会使用 ajax 加载部分节点 一切正常 直到我包含 jquery validate 脚本来验证我的表单
  • 无法在 Flutter 中使用 Firebase Auth 进行注册

    我正在为我的应用程序使用 Firebase 我已正确设置所有内容 并且 Firebase Firestore 工作正常 没有任何问题 我能够在那里读取和写入数据 但是当我尝试在 Firebase 中创建用户时 我在调试控制台中收到此消息 I
  • 如何在 nightwatch.js 中使用链接文本单击链接

    假设我的网页上有这些元素 a href dynamic1 One a a href dynamic2 Two a a href dynamic3 Three a 我想点击带有文字的链接Two 如何使用链接文字没有任何独特的属性 如 id 或
  • typescript Symbol.iterator

    我正在尝试创建一个自定义的可迭代对象 这是我的代码的简化示例 class SortedArray Symbol iterator yield 1 yield 2 yield 3 return 4 const testingIterables
  • 在 std::map 中搜索特定值[重复]

    这个问题在这里已经有答案了 可能的重复 检查 std map 中是否存在值 C https stackoverflow com questions 535317 checking value exist in a stdmap c 如何遍历
  • Unity中的LoadScene()函数什么时候改变场景?

    当您调用函数 LoadScene 时 它是立即切换场景 还是只是表示场景需要更改 LoadScene 的文档没有说 我正在使用的具体示例如下所示 LoadScene levelName ItemBox newBox ItemBox Inst
  • 在 Spring 中将 SOAP 1.2 与 WebServiceGatewaySupport 结合使用

    我对 Spring 框架非常陌生 在使用 Spring 创建一个简单的 SOAP 客户端时遇到了一些问题 像一个好的新手一样 我使用 Spring 教程来制作我的 SOAP 客户端 你可以在这里找到它 gt https spring io