'JAXB unmarshalling exception: unexpected element WebServiceTemplate spring boot

i'm working in a spring boot project where i'm trying to make a SOAP request using WebServiceTemplate but i got the following error :

javax.xml.bind.UnmarshalException: unexpected element (URI : "http://schemas.xmlsoap.org/soap/envelope/", local : "Fault"). the expected elements are <{urn:xtk:session}Logon>,<{urn:xtk:session}LogonResponse>

im trying to find a solution since 3 days but i didn't find it yet. this is my code :

WSDL file :

<?xml version="1.0" encoding="UTF-8"?>
<definitions    xmlns="http://schemas.xmlsoap.org/wsdl/" 
                xmlns:s="http://www.w3.org/2001/XMLSchema" 
                xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" 
                xmlns:tns="urn:xtk:session" 
                xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" 
                targetNamespace="urn:xtk:session">
   <types>
      <s:schema elementFormDefault="qualified" targetNamespace="urn:xtk:session">
         <s:complexType name="Element">
            <s:sequence>
               <s:any processContents="lax" />
            </s:sequence>
         </s:complexType>
         <s:element name="Logon">
            <s:complexType>
               <s:sequence>
                  <s:element maxOccurs="1" minOccurs="1" name="sessiontoken" type="s:string" />
                  <s:element maxOccurs="1" minOccurs="1" name="strLogin" type="s:string" />
                  <s:element maxOccurs="1" minOccurs="1" name="strPassword" type="s:string" />
                  <s:element maxOccurs="1" minOccurs="1" name="elemParameters" type="tns:Element" />
               </s:sequence>
            </s:complexType>
         </s:element>
         <s:element name="LogonResponse">
            <s:complexType>
               <s:sequence>
                  <s:element maxOccurs="1" minOccurs="1" name="pstrSessionToken" type="s:string" />
                  <s:element maxOccurs="1" minOccurs="1" name="pSessionInfo" type="tns:Element" />
                  <s:element maxOccurs="1" minOccurs="1" name="pstrSecurityToken" type="s:string" />
               </s:sequence>
            </s:complexType>
         </s:element>
      </s:schema>
   </types>
   <message name="LogonIn">
      <part element="tns:Logon" name="parameters" />
   </message>
   <message name="LogonOut">
      <part element="tns:LogonResponse" name="parameters" />
   </message>
   <portType name="sessionMethodsSoap">
      <operation name="Logon">
         <input message="tns:LogonIn" />
         <output message="tns:LogonOut" />
      </operation>
   </portType>
   <binding name="sessionMethodsSoap" type="tns:sessionMethodsSoap">
      <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
      <operation name="Logon">
         <soap:operation soapAction="xtk:session#Logon" style="document" />
         <input>
            <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="literal" />
         </input>
         <output>
            <soap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" use="literal" />
         </output>
      </operation>
   </binding>
   <service name="XtkSession">
      <port binding="tns:sessionMethodsSoap" name="sessionMethodsSoap">
         <soap:address location="http://ws-soap-serv.com" />
      </port>
   </service>
</definitions>

Generated Classes :

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "sessiontoken",
    "strLogin",
    "strPassword",
    "elemParameters"
})
@XmlRootElement(name = "Logon")
public class Logon { 
...

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "pstrSessionToken",
    "pSessionInfo",
    "pstrSecurityToken"
})
@XmlRootElement(name = "LogonResponse")
public class LogonResponse {
// attributes...

package-info class :

@javax.xml.bind.annotation.XmlSchema(namespace = "urn:xtk:session", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package com.api.soap;

my config Class :

@Configuration
public class SoapConfig {
    
    @Bean
    public Jaxb2Marshaller marshaller() {
        
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        marshaller.setPackagesToScan(new String[]{
                 "com.api.soap"
        });
        
        return marshaller;  
    }
}

and finally this is my methode to call my the soap WS :

Logon logonRequest = new Logon() ;

logonRequest.setSessiontoken("");
logonRequest.setStrLogin("myusername"); //
logonRequest.setStrPassword("mypassword");  //

webServiceTemplate = new WebServiceTemplate(marshaller);
LogonResponse logonResponse = (LogonResponse) webServiceTemplate.marshalSendAndReceive("http://ws-soap-serv.com",logonRequest);
    

do you have any idea why i'm getting this error please :

javax.xml.bind.UnmarshalException: unexpected element (URI : "http://schemas.xmlsoap.org/soap/envelope/", local : "Fault"). the expected elements are <{urn:xtk:session}Logon>,<{urn:xtk:session}LogonResponse>

Best Regards.



Solution 1:[1]

On your WebServiceTempmate use

template.setCheckConnectionForFault(false);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Henry Ecker