Skip to content

Latest commit

 

History

History
163 lines (142 loc) · 10.1 KB

File metadata and controls

163 lines (142 loc) · 10.1 KB

Spring Boot Soap Web Service Example

Disclaimer: This is a personal summary and interpretation based on a YouTube video. It is not official material and not endorsed by the original creator. All rights remain with the respective creators.

This document summarizes the key takeaways from the video. I highly recommend watching the full video for visual context and coding demonstrations.

Before You Get Started

  • I summarize key points to help you learn and review quickly.
  • Simply click on Ask AI links to dive into any topic you want.

AI-Powered buttons

Teach Me: 5 Years Old | Beginner | Intermediate | Advanced | (reset auto redirect)

Learn Differently: Analogy | Storytelling | Cheatsheet | Mindmap | Flashcards | Practical Projects | Code Examples | Common Mistakes

Check Understanding: Generate Quiz | Interview Me | Refactor Challenge | Assessment Rubric | Next Steps

Introduction to SOAP Web Services in Spring Boot

  • Summary: SOAP-based web services still have a role in modern Java architectures, and Spring Boot makes building them straightforward, similar to RESTful services. The tutorial focuses on creating a simple SOAP service for an online rock-paper-scissors game to track user scores.
  • Key Takeaway/Example: Start with a top-down approach using an XSD file to define methods and data, then generate Java beans and endpoints.
  • Link for More Details: Ask AI: Introduction to SOAP in Spring Boot

Creating the Spring Boot Project

  • Summary: Begin by setting up a new Maven-based Spring Boot project in your IDE, naming it appropriately and adding essential dependencies like Spring Boot DevTools and Spring Web Services for SOAP support.
  • Key Takeaway/Example: Use the group ID com.mcz.spring.soap and version 1.0, then proceed to add configurations.
  • Link for More Details: Ask AI: Creating Spring Boot Project

Defining the XSD Schema

  • Summary: Create an XSD file in the resources folder to outline the web service structure, including requests and responses for getting user scores with wins, losses, and ties.
  • Key Takeaway/Example: The XSD defines elements like getScoreRequest (with user name) and getScoreResponse (with score details), using namespace http://soap.j2ee.mcnz.com.
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://soap.j2ee.mcnz.com" targetNamespace="http://soap.j2ee.mcnz.com">
  <!-- Define getScoreRequest, getScoreResponse, and score elements here -->
</xs:schema>

Configuring POM for JAXB and Code Generation

  • Summary: Update the POM file to include the JAX-WS dependency and a JAXB Maven plugin to generate Java classes from the XSD.
  • Key Takeaway/Example: Add jaxws-rt dependency and the jaxb2-maven-plugin with execution to process score.xsd and generate classes in com.mcnz.j2ee.soap package.
<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>jaxb2-maven-plugin</artifactId>
  <version>3.2.0</version>
  <executions>
    <execution>
      <id>xjc</id>
      <goals><goal>xjc</goal></goals>
      <configuration>
        <sources><source>src/main/resources/score.xsd</source></sources>
        <packageName>com.mcnz.j2ee.soap</packageName>
      </configuration>
    </execution>
  </executions>
</plugin>

Generating Java Classes

  • Summary: After POM updates, build the project to auto-generate Java beans like GetScoreRequest, GetScoreResponse, and Score based on the XSD namespace.
  • Key Takeaway/Example: Classes include fields like username, wins, losses, and ties in the Score object.
  • Link for More Details: Ask AI: Generating Java Classes from XSD

Implementing the SOAP Endpoint

  • Summary: Create an endpoint class to handle incoming SOAP requests, process the user name, and return a score response with mocked data (e.g., database lookup simulation).
  • Key Takeaway/Example: Use @Endpoint and @PayloadRoot annotations, then in the method, check the user and set score values accordingly.
@Endpoint
public class ScoreEndpoint {
  @PayloadRoot(namespace = "http://soap.j2ee.mcnz.com", localPart = "getScoreRequest")
  @ResponsePayload
  public GetScoreResponse getScore(@RequestPayload GetScoreRequest request) {
    GetScoreResponse response = new GetScoreResponse();
    Score score = new Score();
    if (request.getUser().equalsIgnoreCase("scrumptuous")) {
      score.setWins(100);
    } else {
      score.setWins(99);
      score.setLosses(66);
      score.setTies(7);
    }
    score.setUser(request.getUser());
    response.setScore(score);
    return response;
  }
}

Configuring Web Service Servlet

  • Summary: Add a configuration class extending WsConfigurerAdapter to set up the message dispatcher servlet, schema, and default WSDL.
  • Key Takeaway/Example: Enable WS, configure servlet for /ws/*, and define beans for XSD and WSDL.
@Configuration
@EnableWs
public class WebServiceConfiguration 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 = "score")
  public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema scoreSchema) {
    DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    wsdl11Definition.setPortTypeName("ScorePort");
    wsdl11Definition.setLocationUri("/ws");
    wsdl11Definition.setTargetNamespace("http://soap.j2ee.mcnz.com");
    wsdl11Definition.setSchema(scoreSchema);
    return wsdl11Definition;
  }

  @Bean
  public XsdSchema scoreSchema() {
    return new SimpleXsdSchema(new ClassPathResource("score.xsd"));
  }
}

Testing the SOAP Service

  • Summary: Run the application, access the WSDL via browser (localhost:8080/ws/score.wsdl), and test requests using SoapUI by importing the WSDL and sending user names like "scrumptuous" or others to verify responses.
  • Key Takeaway/Example: SoapUI generates XML requests; responses show score data as set in the endpoint.
  • Link for More Details: Ask AI: Testing SOAP Service

About the summarizer

I'm Ali Sol, a Backend Developer. Learn more: