java - 使用 SOAP 响应始终为 null Spring Boot

标签 java spring spring-boot soap

我已经创建了 SOAP 客户端来使用 SOAP Webservices 服务器。

  1. 这是我调用 SOAP 服务器的 Web 服务类

    public class SOAPConnector extends WebServiceGatewaySupport {
    
        public HostCustomerResponse getHost() {
    
            String CData;
    
            String DEBIT_ACCTNO = "800000123800";
    
            HostCustomer request = new HostCustomer();
            Input input = new Input();
            input.setCorpID("IDTITOUAT123");
            input.setRequestID("1705547561");
            input.setServiceCode("ACCOUNT_BALANCE");
            input.setTokenAuth("c29c1704547fa0e2ceeabc8099bb7a4510dc0cf535d332fc75faeadcce14510b");
            CData = "<![CDATA["+
                    "<balanceRequest>"+
                    "<balance>"+
                        "<accountNo>"+DEBIT_ACCTNO+"</accountNo>"+
                    "</balance>"+
                "</balanceRequest>"+
                "]]>";
    
            input.setTxnData(CData);
            input.setTxnRequestDateTime("20180904144630");
    
            request.setInput(input);
    
            HostCustomerResponse response = (HostCustomerResponse) getWebServiceTemplate().marshalSendAndReceive(request, new SoapActionCallback("http://pguat2.cimbniaga.co.id:8004/PrismaGateway/services/HostCustomer"));        
            System.out.println(response.getOutput().getTxnData());
            return response;
        }
    }
    
  2. 其他类别分离

    public class Config {
    
        @Bean
        public Jaxb2Marshaller marshaller() {
            Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
            marshaller.setContextPaths("prismagateway_service","prismagateway_param");
            return marshaller;
        }
    
    
        @Bean
        public SOAPConnector soapConnector(Jaxb2Marshaller marshaller) {
            SOAPConnector client = new SOAPConnector();
            client.setDefaultUri("http://pguat2.cimbniaga.co.id:8004/PrismaGateway/services/HostCustomer");
            client.setMarshaller(marshaller);
            client.setUnmarshaller(marshaller);
            return client;
        }
    }
    
  3. 套餐 class generated from wsdl file

  4. Wsdl 类

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://10.25.112.133" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://10.25.112.133" xmlns:intf="http://10.25.112.133" xmlns:tns1="java:prismagateway.service.HostCustomer" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- WSDL created by PrismaGateway -->
 <wsdl:types>
  <schema elementFormDefault="qualified" targetNamespace="http://10.25.112.133" xmlns="http://www.w3.org/2001/XMLSchema">
   <import namespace="java:prismagateway.service.HostCustomer"/>
   <element name="HostCustomer">
    <complexType>
     <sequence>
      <element name="input" type="tns1:Input"/>
     </sequence>
    </complexType>
   </element>
   <element name="HostCustomerResponse">
    <complexType>
     <sequence>
      <element name="output" type="tns1:Output"/>
     </sequence>
    </complexType>
   </element>
  </schema>
  <schema elementFormDefault="qualified" targetNamespace="java:prismagateway.service.HostCustomer" xmlns="http://www.w3.org/2001/XMLSchema">
   <complexType name="Input">
    <sequence>
     <element name="tokenAuth" type="xsd:string"/>
     <element name="txnData" type="xsd:string"/>
     <element name="serviceCode" type="xsd:string"/>
     <element name="corpID" type="xsd:string"/>
     <element name="requestID" type="xsd:string"/>
     <element name="txnRequestDateTime" type="xsd:string"/>
    </sequence>
   </complexType>
   <complexType name="Output">
    <sequence>
     <element name="bankReffNo" nillable="true" type="xsd:string"/>
     <element name="statusCode" nillable="true" type="xsd:string"/>
     <element name="statusMsg" nillable="true" type="xsd:string"/>
     <element name="txnResponseDateTime" nillable="true" type="xsd:string"/>
     <element name="txnData" nillable="true" type="xsd:string"/>
    </sequence>
   </complexType>
  </schema>
 </wsdl:types>
   <wsdl:message name="HostCustomerResponse">
      <wsdl:part element="impl:HostCustomerResponse" name="parameters"/>
   </wsdl:message>
   <wsdl:message name="HostCustomerRequest">
      <wsdl:part element="impl:HostCustomer" name="parameters"/>
   </wsdl:message>
   <wsdl:portType name="HostCustomer">
      <wsdl:operation name="HostCustomer">
         <wsdl:input message="impl:HostCustomerRequest" name="HostCustomerRequest"/>
         <wsdl:output message="impl:HostCustomerResponse" name="HostCustomerResponse"/>
      </wsdl:operation>
   </wsdl:portType>
   <wsdl:binding name="HostCustomerSoapBinding" type="impl:HostCustomer">
      <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
      <wsdl:operation name="HostCustomer">
         <wsdlsoap:operation soapAction=""/>
         <wsdl:input name="HostCustomerRequest">
            <wsdlsoap:body use="literal"/>
         </wsdl:input>
         <wsdl:output name="HostCustomerResponse">
            <wsdlsoap:body use="literal"/>
         </wsdl:output>
      </wsdl:operation>
   </wsdl:binding>
   <wsdl:service name="HostCustomer">
      <wsdl:port binding="impl:HostCustomerSoapBinding" name="HostCustomer">
         <wsdlsoap:address location="http://pguat2.cimbniaga.co.id:8004/PrismaGateway/services/HostCustomer"/>
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>

  • 这是控制台响应 问题是我已经在控制台logging.level.org.springframework.ws=TRACE中得到Soap Response,但当我在上面的//System.out.println(response.getOutput().getTxnData());//SOAPConnectorClass 中打印时始终为 Null 。怎么可能?
  •   .   ____          _            __ _ _
     /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
    ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
     \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.4.1.RELEASE)
    
    2018-09-06 11:23:19.262  INFO 10340 --- [           main] s.SpringBootSoapClientApplication        : Starting SpringBootSoapClientApplication on DESKTOP-7CF3AR1 with PID 10340 (D:\SpringWorkspace\spring-boot-soap\target\classes started by KPT-IT-EZRA in D:\SpringWorkspace\spring-boot-soap)
    2018-09-06 11:23:19.268  INFO 10340 --- [           main] s.SpringBootSoapClientApplication        : No active profile set, falling back to default profiles: default
    2018-09-06 11:23:19.321  INFO 10340 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1a4927d6: startup date [Thu Sep 06 11:23:19 ICT 2018]; root of context hierarchy
    2018-09-06 11:23:20.248  INFO 10340 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.ws.config.annotation.DelegatingWsConfiguration' of type [class org.springframework.ws.config.annotation.DelegatingWsConfiguration$$EnhancerBySpringCGLIB$$5f57ab1c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
    2018-09-06 11:23:20.284  INFO 10340 --- [           main] .w.s.a.s.AnnotationActionEndpointMapping : Supporting [WS-Addressing August 2004, WS-Addressing 1.0]
    2018-09-06 11:23:20.751  INFO 10340 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 9090 (http)
    2018-09-06 11:23:20.764  INFO 10340 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
    2018-09-06 11:23:20.766  INFO 10340 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.5
    2018-09-06 11:23:20.872  INFO 10340 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
    2018-09-06 11:23:20.873  INFO 10340 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1556 ms
    2018-09-06 11:23:21.068  INFO 10340 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
    2018-09-06 11:23:21.070  INFO 10340 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'messageDispatcherServlet' to [/services/*]
    2018-09-06 11:23:21.074  INFO 10340 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
    2018-09-06 11:23:21.075  INFO 10340 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
    2018-09-06 11:23:21.075  INFO 10340 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
    2018-09-06 11:23:21.075  INFO 10340 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
    2018-09-06 11:26:04.810  INFO 10340 --- [           main] o.s.oxm.jaxb.Jaxb2Marshaller             : Creating JAXBContext with context path [_133._112._25._10:prismagateway_service]
    2018-09-06 11:27:14.463  INFO 10340 --- [           main] o.s.ws.soap.saaj.SaajSoapMessageFactory  : Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
    2018-09-06 11:27:14.471 DEBUG 10340 --- [           main] o.s.ws.soap.saaj.SaajSoapMessageFactory  : Using MessageFactory class [com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl]
    2018-09-06 11:27:59.795  INFO 10340 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1a4927d6: startup date [Thu Sep 06 11:23:19 ICT 2018]; root of context hierarchy
    2018-09-06 11:28:00.066  INFO 10340 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
    2018-09-06 11:28:00.069  INFO 10340 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
    2018-09-06 11:28:00.276  INFO 10340 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-09-06 11:28:00.276  INFO 10340 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-09-06 11:28:00.558  INFO 10340 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
    2018-09-06 11:28:01.090 DEBUG 10340 --- [           main] yloadRootAnnotationMethodEndpointMapping : Looking for endpoints in application context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1a4927d6: startup date [Thu Sep 06 11:23:19 ICT 2018]; root of context hierarchy
    2018-09-06 11:28:01.138 DEBUG 10340 --- [           main] oapActionAnnotationMethodEndpointMapping : Looking for endpoints in application context: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1a4927d6: startup date [Thu Sep 06 11:23:19 ICT 2018]; root of context hierarchy
    2018-09-06 11:28:01.205 DEBUG 10340 --- [           main] o.s.w.s.e.a.DefaultMethodEndpointAdapter : No MethodArgumentResolvers set, using defaults: [org.springframework.ws.server.endpoint.adapter.method.dom.DomPayloadMethodProcessor@124ac145, org.springframework.ws.server.endpoint.adapter.method.MessageContextMethodArgumentResolver@2def7a7a, org.springframework.ws.server.endpoint.adapter.method.SourcePayloadMethodProcessor@24e83d19, org.springframework.ws.server.endpoint.adapter.method.XPathParamMethodArgumentResolver@5c080ef3, org.springframework.ws.soap.server.endpoint.adapter.method.SoapMethodArgumentResolver@188cbcde, org.springframework.ws.soap.server.endpoint.adapter.method.SoapHeaderElementMethodArgumentResolver@4ee6291f, org.springframework.ws.server.endpoint.adapter.method.jaxb.XmlRootElementPayloadMethodProcessor@2b03d52f, org.springframework.ws.server.endpoint.adapter.method.jaxb.JaxbElementPayloadMethodProcessor@37e0292a, org.springframework.ws.server.endpoint.adapter.method.StaxPayloadMethodArgumentResolver@4af70944]
    2018-09-06 11:28:01.210 DEBUG 10340 --- [           main] o.s.w.s.e.a.DefaultMethodEndpointAdapter : No MethodReturnValueHandlers set, using defaults: [org.springframework.ws.server.endpoint.adapter.method.dom.DomPayloadMethodProcessor@35267fd4, org.springframework.ws.server.endpoint.adapter.method.SourcePayloadMethodProcessor@397ef2, org.springframework.ws.server.endpoint.adapter.method.jaxb.XmlRootElementPayloadMethodProcessor@36a6bea6, org.springframework.ws.server.endpoint.adapter.method.jaxb.JaxbElementPayloadMethodProcessor@44e93c1f]
    2018-09-06 11:28:01.748  INFO 10340 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    2018-09-06 11:28:44.850  INFO 10340 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 9090 (http)
    2018-09-06 11:28:56.622  INFO 10340 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5f7989fa: startup date [Thu Sep 06 11:28:56 ICT 2018]; root of context hierarchy
    2018-09-06 11:29:06.116  INFO 10340 --- [           main] o.s.oxm.jaxb.Jaxb2Marshaller             : Creating JAXBContext with context path [_133._112._25._10:prismagateway_service]
    2018-09-06 11:29:16.561  INFO 10340 --- [           main] o.s.ws.soap.saaj.SaajSoapMessageFactory  : Creating SAAJ 1.3 MessageFactory with SOAP 1.1 Protocol
    2018-09-06 11:29:16.564 DEBUG 10340 --- [           main] o.s.ws.soap.saaj.SaajSoapMessageFactory  : Using MessageFactory class [com.sun.xml.internal.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl]
    2018-09-06 11:29:24.733 DEBUG 10340 --- [           main] o.s.ws.client.core.WebServiceTemplate    : Opening [org.springframework.ws.transport.http.HttpUrlConnection@5f0f9947] to [http://pguat2.cimbniaga.co.id:8004/PrismaGateway/services/HostCustomer]
    2018-09-06 11:29:25.202 TRACE 10340 --- [           main] o.s.ws.client.MessageTracing.sent        : Sent request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns3:HostCustomer xmlns:ns2="java:prismagateway.service.HostCustomer" xmlns:ns3="http://10.25.112.133"><ns3:input><ns2:tokenAuth>c29c1704547fa0e2ceeabc8099bb7a4510dc0cf535d332fc75faeadcce14510b</ns2:tokenAuth><ns2:txnData>&lt;![CDATA[&lt;balanceRequest&gt;&lt;balance&gt;&lt;accountNo&gt;800000123800&lt;/accountNo&gt;&lt;/balance&gt;&lt;/balanceRequest&gt;]]&gt;</ns2:txnData><ns2:serviceCode>ACCOUNT_BALANCE</ns2:serviceCode><ns2:corpID>IDTITOUAT123</ns2:corpID><ns2:requestID>1705547561</ns2:requestID><ns2:txnRequestDateTime>20180904144630</ns2:txnRequestDateTime></ns3:input></ns3:HostCustomer></SOAP-ENV:Body></SOAP-ENV:Envelope>]
    2018-09-06 11:29:35.873 TRACE 10340 --- [           main] o.s.ws.client.MessageTracing.received    : Received response [<?xml version="1.0" encoding="UTF-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><HostCustomerResponse xmlns="http://10.25.112.133"><ns1:output xmlns:ns1="http://10.25.136.152" xmlns:ns2="java:prismagateway.service.HostCustomer" xsi:type="ns2:Output"><ns2:bankReffNo xsi:nil="true"/><ns2:statusCode>008</ns2:statusCode><ns2:statusMsg>Request message is expired</ns2:statusMsg><ns2:txnResponseDateTime>20180906112726</ns2:txnResponseDateTime><ns2:txnData>&lt;![CDATA[&lt;balanceRequest&gt;&lt;balance&gt;&lt;accountNo&gt;800000123800&lt;/accountNo&gt;&lt;/balance&gt;&lt;/balanceRequest&gt;]]&gt;</ns2:txnData></ns1:output></HostCustomerResponse></soapenv:Body></soapenv:Envelope>] for request [<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><ns3:HostCustomer xmlns:ns2="java:prismagateway.service.HostCustomer" xmlns:ns3="http://10.25.112.133"><ns3:input><ns2:tokenAuth>c29c1704547fa0e2ceeabc8099bb7a4510dc0cf535d332fc75faeadcce14510b</ns2:tokenAuth><ns2:txnData>&lt;![CDATA[&lt;balanceRequest&gt;&lt;balance&gt;&lt;accountNo&gt;800000123800&lt;/accountNo&gt;&lt;/balance&gt;&lt;/balanceRequest&gt;]]&gt;</ns2:txnData><ns2:serviceCode>ACCOUNT_BALANCE</ns2:serviceCode><ns2:corpID>IDTITOUAT123</ns2:corpID><ns2:requestID>1705547561</ns2:requestID><ns2:txnRequestDateTime>20180904144630</ns2:txnRequestDateTime></ns3:input></ns3:HostCustomer></SOAP-ENV:Body></SOAP-ENV:Envelope>]
    2018-09-06 11:39:21.690  INFO 10340 --- [           main] utoConfigurationReportLoggingInitializer : 
    
    Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
    2018-09-06 11:39:21.919 ERROR 10340 --- [           main] o.s.boot.SpringApplication               : Application startup failed
    
    java.lang.IllegalStateException: Failed to execute CommandLineRunner
    	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:803) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    	at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    	at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:771) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    	at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    	at springbootsoapclient.SpringBootSoapClientApplication.main(SpringBootSoapClientApplication.java:23) [classes/:na]
    Caused by: java.lang.NullPointerException: null
    	at springbootsoapclient.SpringBootSoapClientApplication.lambda$0(SpringBootSoapClientApplication.java:81) [classes/:na]
    	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-1.4.1.RELEASE.jar:1.4.1.RELEASE]
    	... 6 common frames omitted

    最佳答案

    看起来像下面的行导致了问题。

    System.out.println(response.getOutput().getTxnData());
    

    所以请像这样更改

    if(response!=null && response.getOutput()!=null){
       System.out.println(response.getOutput().getTxnData());
    }
    

    关于java - 使用 SOAP 响应始终为 null Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52177114/

    相关文章:

    java - Spring JPA - "java.lang.IllegalArgumentException: Projection type must be an interface!"(使用 native 查询)

    Java: View (GUI)和 Controller 与 MVC 的交互?

    java - FPS 锁定不精确

    java - 从两个不同的 SQL 表中返回结果作为搜索结果

    spring - 如何在 Grails Spock 单元测试中初始化/连接 bean?

    spring - JUnit测试用例错误: "Address already in use: bind"

    java - 在android中解析数据到listview

    java - 连接 Mongo 时出现身份验证错误

    java - 如何使用 JPA 查询在同一字段中选择可变数量的相似字符串?

    spring - Spring Boot Cloud Kubernetes配置不适用于多个Pod