Quick revision
The short answer
Contract first means we write the XSD schema first and generate Java classes from it with a JAXB plugin, using the jakarta.xml.bind packages on Spring Boot 3 and later. We add spring-boot-starter-web-services, which registers the MessageDispatcherServlet, publish the WSDL with a DefaultWsdl11Definition bean built from the XSD, and write an @Endpoint class whose @PayloadRoot methods take a @RequestPayload and return a @ResponsePayload. Faults come from exceptions annotated with @SoapFault, and security and validation are added as interceptors, such as Wss4jSecurityInterceptor and PayloadValidatingInterceptor.
What to remember
In this answer, we built a contract-first SOAP endpoint with Spring Web Services. We wrote the XSD, generated JAXB classes in the jakarta.xml.bind packages, added spring-boot-starter-web-services so Spring Boot registers MessageDispatcherServlet, and published the WSDL with a DefaultWsdl11Definition built from the schema. The @Endpoint class handles operations with @PayloadRoot methods, @RequestPayload parameters and @ResponsePayload return values. Faults come from @SoapFault exceptions resolved by SoapFaultAnnotationExceptionResolver, requests are validated with PayloadValidatingInterceptor, and WS-Security is added with Wss4jSecurityInterceptor.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Contract first means we write the XSD schema first and generate Java classes from it with a JAXB plugin, using the jakarta.xml.bind packages on Spring Boot 3 and later. We add spring-boot-starter-web-services, which registers the MessageDispatcherServlet, publish the WSDL with a DefaultWsdl11Definition bean built from the XSD, and write an @Endpoint class whose @PayloadRoot methods take a @RequestPayload and return a @ResponsePayload. Faults come from exceptions annotated with @SoapFault, and security and validation are added as interceptors, such as Wss4jSecurityInterceptor and PayloadValidatingInterceptor.
Why this question is asked
SOAP is not fashionable, but it is still everywhere in banking, insurance, telecoms and government integrations, and many Java developers meet it for the first time when a partner hands them a WSDL. Interviewers ask this question to check that we know the Spring Web Services way of doing it, which is deliberately contract first, and that we can name the moving parts. In this answer, we will recap what a SOAP contract is, write the XSD, generate the classes, configure the servlet and WSDL, write the endpoint, and then add faults, validation and security.
1A Quick Recap of SOAP and Contract First
SOAP is a protocol for exchanging XML messages. Each message is an envelope with an optional header, for things like security tokens, and a body that holds the actual request or response. The structure of those bodies is described by an XSD, an XML Schema, and the operations, addresses and bindings by a WSDL document, which a partner can feed into a tool to generate a client.
There are two ways to arrive at that contract. Code first starts from Java classes and lets a framework generate the WSDL. Contract first starts from the XSD, agreed with the partner, and generates or writes the Java code to fit it. Spring Web Services supports only contract first, on purpose, for reasons we will see in the FAQ.
2Writing the XSD and Generating Classes
We start with the schema. Spring Web Services finds operations by the name of the request element, so the convention is a Request and a Response element for each operation.
Then a JAXB code generation plugin in the build, such as the jaxb2 Maven plugin or the jaxb Maven plugin from the JAXB tools project, reads the XSD and generates GetBalanceRequest and GetBalanceResponse classes with JAXB annotations. JAXB is the Java API that maps XML to objects. On Spring Boot 3 and later, which use Jakarta EE, the generated classes must use the jakarta.xml.bind packages; the old javax.xml.bind packages are legacy and will not work with the Jakarta runtime. Plugin versions differ in which namespace they generate, so check your plugin version.
3Registering the Servlet and Publishing the WSDL
With spring-boot-starter-web-services on the classpath, Spring Boot registers the MessageDispatcherServlet, the front controller of Spring Web Services, at /services/* by default (spring.webservices.path changes it). To publish a WSDL generated from our XSD, we add a DefaultWsdl11Definition bean; it needs the wsdl4j library on the classpath.
Notice that the WSDL is derived from the schema: DefaultWsdl11Definition finds the elements ending in Request and Response and builds operations from them. If the partner already gave us a finished WSDL, we can publish that file as it is instead, with SimpleWsdl11Definition or the spring.webservices.wsdl-locations property.
4Writing the Endpoint
The endpoint is the SOAP equivalent of a controller. The class is marked @Endpoint, and each method says which request element it handles with @PayloadRoot, giving the namespace and the local name of the element.
6 messages in order. Play them, or step through them one at a time.
@RequestPayload tells Spring to unmarshal the SOAP body into the generated class, and @ResponsePayload tells it to marshal our return value into the response body. The envelope itself is handled for us.
5Faults, Validation and Security
When something goes wrong, SOAP clients expect a SOAP fault, not an HTML error page. The simplest way is to annotate our exception with @SoapFault; SoapFaultAnnotationExceptionResolver, registered by default, turns it into a fault with the given code and the exception message.
PayloadValidatingInterceptor checks every request against the XSD before our endpoint sees it, so a missing accountId returns a clear fault instead of a NullPointerException. For security, many partners require WS-Security, a standard for signing, encrypting and attaching tokens inside the SOAP header. Spring Web Services supports it through Wss4jSecurityInterceptor, built on Apache WSS4J, configured with the actions the partner expects, such as Timestamp Signature, and the keystores. We can still use transport security with TLS and client certificates alongside it.
6Follow-up questions
Why is contract-first recommended over generating WSDL from Java?
Because the contract is what partners depend on, and generating it from Java ties it to implementation details. A renamed field, a new Java type or a framework upgrade can silently change the generated WSDL and break clients. Java types also do not map cleanly to XML Schema: a Date, a Map or an inheritance hierarchy can produce awkward or non-portable XSD. Starting from the XSD keeps the contract stable, reviewable and language-neutral, lets us use schema features such as restrictions and patterns for validation, and lets the Java code change freely behind it.
Do I need to write the WSDL by hand?
No. With DefaultWsdl11Definition, Spring generates the WSDL from the XSD at runtime by convention. We only write a full WSDL when the partner requires a specific one, in which case we publish their file unchanged.
How do I test a SOAP endpoint?
Spring Web Services has MockWebServiceClient for server-side tests: we send a request payload from an XML file and assert on the response payload or fault. Using real sample messages from the partner as test inputs is the best protection against surprises.
7Conclusion
In this answer, we built a contract-first SOAP endpoint with Spring Web Services. We wrote the XSD, generated JAXB classes in the jakarta.xml.bind packages, added spring-boot-starter-web-services so Spring Boot registers MessageDispatcherServlet, and published the WSDL with a DefaultWsdl11Definition built from the schema. The @Endpoint class handles operations with @PayloadRoot methods, @RequestPayload parameters and @ResponsePayload return values. Faults come from @SoapFault exceptions resolved by SoapFaultAnnotationExceptionResolver, requests are validated with PayloadValidatingInterceptor, and WS-Security is added with Wss4jSecurityInterceptor.
Open with
Spring Web Services is contract first, so I start with the XSD, agreed with the partner, with a Request and a Response element for each operation. A JAXB plugin in the build generates the Java classes from the schema, and on Spring Boot 3 I make sure they use the jakarta.xml.bind packages, since javax JAXB is legacy.
Then, if they want more
Then I add spring-boot-starter-web-services, which registers the MessageDispatcherServlet under /services by default. To publish the WSDL, I define a DefaultWsdl11Definition bean with a port type name, the location URI, the target namespace and an XsdSchema loaded from the XSD, and I need wsdl4j on the classpath. Spring builds the WSDL from the Request and Response elements.
The endpoint is a class annotated with @Endpoint, and each method has @PayloadRoot with the namespace and the local name of the request element, takes the generated request class as a @RequestPayload parameter and returns the response class with @ResponsePayload. For errors, I annotate exceptions with @SoapFault so SoapFaultAnnotationExceptionResolver turns them into proper SOAP faults.
And I register interceptors: PayloadValidatingInterceptor to validate requests against the schema, and Wss4jSecurityInterceptor when the partner requires WS-Security signatures or tokens.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Framework reference, Spring Web MVC
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How do you build a contract-first SOAP endpoint in Spring Boot with Spring Web Services?...": https://scrivio-interview-library.netlify.app/spring-boot/web-other/d93314a008/