Exploring the spring-boot-starter-web-services Starter in Spring Boot

Spring Boot simplifies the development of web services by providing a comprehensive set of tools and libraries. One such tool is the spring-boot-starter-web-services starter, which streamlines the creation and consumption of SOAP web services. In this blog post, we’ll delve into the features of this starter and provide practical examples to help you get started.

What is spring-boot-starter-web-services?

The spring-boot-starter-web-services starter is a convenient dependency that bundles all the necessary components to build SOAP-based web services. It includes Spring Web Services, which provides a robust framework for creating contract-first SOAP web services.

Key Features

  1. Auto-Configuration: Automatically configures Spring Web Services components.
  2. WSDL and XSD Support: Simplifies the creation of WSDL and XSD files.
  3. WebServiceTemplate: Facilitates calling remote web services.
  4. SOAP Message Handling: Provides tools for handling SOAP messages.

Adding the Dependency

To use the spring-boot-starter-web-services starter, add the following dependency to your pom.xml:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web-services</artifactId>
</dependency>

Creating a Simple SOAP Web Service

Let’s create a simple SOAP web service that provides a greeting message.

  1. Define the WSDL

Create a WSDL file (greeting.wsdl) in the src/main/resources/wsdl directory:

<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
             xmlns:tns="http://example.com/greeting"
             xmlns:xsd="http://www.w3.org/2001/XMLSchema"
             targetNamespace="http://example.com/greeting">
    <message name="GreetingRequest">
        <part name="name" type="xsd:string"/>
    </message>
    <message name="GreetingResponse">
        <part name="message" type="xsd:string"/>
    </message>
    <portType name="GreetingPortType">
        <operation name="greet">
            <input message="tns:GreetingRequest"/>
            <output message="tns:GreetingResponse"/>
        </operation>
    </portType>
    <binding name="GreetingBinding" type="tns:GreetingPortType">
        <soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
        <operation name="greet">
            <soap:operation soapAction="http://example.com/greet"/>
            <input>
                <soap:body use="literal"/>
            </input>
            <output>
                <soap:body use="literal"/>
            </output>
        </operation>
    </binding>
    <service name="GreetingService">
        <port name="GreetingPort" binding="tns:GreetingBinding">
            <soap:address location="http://localhost:8080/ws"/>
        </port>
    </service>
</definitions>
  1. Configure the Web Service

Create a configuration class to define the WSDL location:

@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

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

    @Bean(name = "greeting")
    public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema greetingSchema) {
        DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
        wsdl11Definition.setPortTypeName("GreetingPort");
        wsdl11Definition.setLocationUri("/ws");
        wsdl11Definition.setTargetNamespace("http://example.com/greeting");
        wsdl11Definition.setSchema(greetingSchema);
        return wsdl11Definition;
    }

    @Bean
    public XsdSchema greetingSchema() {
        return new SimpleXsdSchema(new ClassPathResource("wsdl/greeting.xsd"));
    }
}
  1. Implement the Endpoint

Create an endpoint to handle the SOAP requests:

@Endpoint
public class GreetingEndpoint {

    private static final String NAMESPACE_URI = "http://example.com/greeting";

    @PayloadRoot(namespace = NAMESPACE_URI, localPart = "GreetingRequest")
    @ResponsePayload
    public GreetingResponse greet(@RequestPayload GreetingRequest request) {
        GreetingResponse response = new GreetingResponse();
        response.setMessage("Hello, " + request.getName() + "!");
        return response;
    }
}
  1. Run the Application

Run your Spring Boot application, and your SOAP web service will be available at http://localhost:8080/ws.

Consuming a SOAP Web Service

To consume a SOAP web service, you can use the WebServiceTemplate class. Here’s an example:

@Service
public class GreetingClient {

    private final WebServiceTemplate webServiceTemplate;

    public GreetingClient(WebServiceTemplateBuilder webServiceTemplateBuilder) {
        this.webServiceTemplate = webServiceTemplateBuilder.build();
    }

    public GreetingResponse getGreeting(String name) {
        GreetingRequest request = new GreetingRequest();
        request.setName(name);
        return (GreetingResponse) webServiceTemplate.marshalSendAndReceive(request, new SoapActionCallback("http://example.com/greet"));
    }
}

Conclusion

The spring-boot-starter-web-services starter makes it easy to create and consume SOAP web services in Spring Boot. By leveraging the auto-configuration and powerful tools provided by Spring Web Services, you can quickly build robust web services with minimal configuration.

Happy coding! 🚀

Post a Comment

Previous Post Next Post