Search code examples
springspring-bootdependency-injectioninversion-of-controlspring-ws

SPRING-WS No marshaller registered. Caused by SPRING IOC


I have a SOAP client service which works fine. The SOAP headers and request are managed in a SOAPConnector class.

public class SOAPConnector extends WebServiceGatewaySupport {

    public Object callWebService(String url, Object request) {
        // CREDENTIALS and REQUEST SETTINGS...
        return getWebServiceTemplate().marshalSendAndReceive(url, request, new SetHeader(requestHeader));
    }
}

I'm receiving the requested Data once I call my (SoapConnector) service on the main Class.

@SpringBootApplication
public class SpringSoapSecurityDemoApplication {

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

    @Bean
    public CommandLineRunner lookup(SOAPConnector soapConnector) {
        return args -> {
            String hotelCode = "****";
            FutureBookingSummaryRequest request = new FutureBookingSummaryRequest();
            FetchBookingFilters additionalFilters = new FetchBookingFilters();
           // Some additionalFilters settings
            request.setAdditionalFilters(additionalFilters);
            FutureBookingSummaryResponse response = (FutureBookingSummaryResponse) soapConnector
                    .callWebService("MY WSDL URL", request);
            System.err.println(response.getHotelReservations());
        };
    }
}

SO FAR IT WORKS FINE.

Then I've tried to create a separate service for the previous request. BookingService.java

public class BookingService extends WebServiceGatewaySupport {
    @Autowired
    SOAPConnector soapConnector;

    public String getReservations() {
        String hotelCode = "****";
        FutureBookingSummaryRequest request = new FutureBookingSummaryRequest();
        FetchBookingFilters additionalFilters = new FetchBookingFilters();
       // Some additionalFilters settings
        request.setAdditionalFilters(additionalFilters);
        FutureBookingSummaryResponse response = (FutureBookingSummaryResponse) soapConnector
                .callWebService("MY WSDL URL", request);
        System.err.println(response.getHotelReservations());
    };}

In order to inject the SOAPCONNECTOR I've added @Service to SOAPCONNECTOR class , and @Autowired SOAPConnector soapConnector to the service calling it
Now once I call the created BookingService in the main class

@SpringBootApplication
public class SpringSoapSecurityDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringSoapSecurityDemoApplication.class, args);
        BookingService bookingService = new BookingService();
        bookingService.getReservations();
    } 
}

The SOAPCONNECTOR stops working an I receive the following error :

No marshaller registered. Check configuration of WebServiceTemplate.

I'm pretty sure that's this issue is related to SPRING IOC , dependecy injection .. Since the SOAP service is well configured and working..

Note : I've checked this similiar question Consuming a SOAP web service error (No marshaller registered. Check configuration of WebServiceTemplate) but the @Autowired didn't solve the issue. Any help ?


Solution

  • In case someone is facing the same issue, it turned out that I've missed the @Configuration annotation on the beans configuration class. The right one should look like the following:

        @Configuration
    public class ConsumerConfig {
    
        private String ContextPath = "somePackage";
        private String DefaultUri = "someWsdlURI";
    
        @Bean
        public Jaxb2Marshaller marshaller() {
            Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
            // this package must match the package in the <generatePackage> specified in
            // pom.xml
            marshaller.setContextPath(ContextPath);
            return marshaller;
        }
    
        @Bean
        public SOAPConnector checkFutureBookingSummary(Jaxb2Marshaller marshaller) {
            SOAPConnector client = new SOAPConnector();
            client.setDefaultUri(DefaultUri);
            client.setMarshaller(marshaller);
            client.setUnmarshaller(marshaller);
            return client;
        }