添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Producing a SOAP web service

This guide walks you through the process of creating a SOAP-based web service server with Spring.

What You Will build

You will build a server that exposes data from various European countries by using a WSDL-based SOAP web service.

  • Java 17 or later

  • Gradle 7.5+ or Maven 3.5+

  • You can also import the code straight into your IDE:

  • Spring Tool Suite (STS)

  • IntelliJ IDEA

  • VSCode

  • Like most Spring Getting Started guides , you can start from scratch and complete each step or you can bypass basic setup steps that are already familiar to you. Either way, you end up with working code.

    To start from scratch , move on to Starting with Spring Initializr .

    To skip the basics , do the following:

  • Download and unzip the source repository for this guide, or clone it using Git : git clone https://github.com/spring-guides/gs-soap-service.git

  • cd into gs-soap-service/initial

  • Jump ahead to Add the Spring-WS dependency .

  • When you finish , you can check your results against the code in gs-soap-service/complete .

    You can use this pre-initialized project and click Generate to download a ZIP file. This project is configured to fit the examples in this tutorial.

    To manually initialize the project:

  • Navigate to https://start.spring.io . This service pulls in all the dependencies you need for an application and does most of the setup for you.

  • Choose either Gradle or Maven and the language you want to use. This guide assumes that you chose Java.

  • Click Dependencies and select Spring Web and Spring Web Services .

  • Click Generate .

  • Download the resulting ZIP file, which is an archive of a web application that is configured with your choices.

  • The project needs to include spring-ws-core and wsdl4j as dependencies in your build file.

    The following example shows the changes you need to make to the pom.xml file if you use Maven:

    dependencies {
    	implementation 'org.springframework.boot:spring-boot-starter-web'
    	implementation 'org.springframework.boot:spring-boot-starter-web-services'
    	implementation 'wsdl4j:wsdl4j'
    	jaxb("org.glassfish.jaxb:jaxb-xjc")
    	testImplementation('org.springframework.boot:spring-boot-starter-test')
       

    The web service domain is defined in an XML schema file (XSD) that Spring-WS will automatically export as a WSDL.

    Create an XSD file with operations to return a country’s name, population, capital, and currency. The following listing (from src/main/resources/countries.xsd) shows the necessary XSD file:

    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://spring.io/guides/gs-producing-web-service"
               targetNamespace="http://spring.io/guides/gs-producing-web-service" elementFormDefault="qualified">
        <xs:element name="getCountryRequest">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="name" type="xs:string"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:element name="getCountryResponse">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="country" type="tns:country"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
        <xs:complexType name="country">
            <xs:sequence>
                <xs:element name="name" type="xs:string"/>
                <xs:element name="population" type="xs:int"/>
                <xs:element name="capital" type="xs:string"/>
                <xs:element name="currency" type="tns:currency"/>
            </xs:sequence>
        </xs:complexType>
        <xs:simpleType name="currency">
            <xs:restriction base="xs:string">
                <xs:enumeration value="GBP"/>
                <xs:enumeration value="EUR"/>
                <xs:enumeration value="PLN"/>
            </xs:restriction>
        </xs:simpleType>
    </xs:schema>

    The next step is to generate Java classes from the XSD file. The right approach is to do this automatically during build time by using a Maven or Gradle plugin.

    The following listing shows the necessary plugin configuration for Maven:

    <groupId>org.codehaus.mojo</groupId> <artifactId>jaxb2-maven-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>xjc</id> <goals> <goal>xjc</goal> </goals> </execution> </executions> <configuration> <sources> <source>${project.basedir}/src/main/resources/countries.xsd</source> </sources> </configuration> </plugin>
    task genJaxb { ext.sourcesDir = "${buildDir}/generated-sources/jaxb" ext.schema = "src/main/resources/countries.xsd" outputs.dir sourcesDir doLast() { project.ant { taskdef name: "xjc", classname: "com.sun.tools.xjc.XJCTask", classpath: configurations.jaxb.asPath mkdir(dir: sourcesDir) xjc(destdir: sourcesDir, schema: schema) { arg(value: "-wsdl") produces(dir: sourcesDir, includes: "**/*.java") compileJava.dependsOn genJaxb

    Because Gradle does not have a JAXB plugin (yet), it involves an Ant task, which makes it a bit more complex than in Maven.

    In both cases, the JAXB domain object generation process has been wired into the build tool’s lifecycle, so there are no extra steps to run.

    import io.spring.guides.gs_producing_web_service.Country; import io.spring.guides.gs_producing_web_service.Currency; import org.springframework.stereotype.Component; import org.springframework.util.Assert; @Component public class CountryRepository { private static final Map<String, Country> countries = new HashMap<>(); @PostConstruct public void initData() { Country spain = new Country(); spain.setName("Spain"); spain.setCapital("Madrid"); spain.setCurrency(Currency.EUR); spain.setPopulation(46704314); countries.put(spain.getName(), spain); Country poland = new Country(); poland.setName("Poland"); poland.setCapital("Warsaw"); poland.setCurrency(Currency.PLN); poland.setPopulation(38186860); countries.put(poland.getName(), poland); Country uk = new Country(); uk.setName("United Kingdom"); uk.setCapital("London"); uk.setCurrency(Currency.GBP); uk.setPopulation(63705000); countries.put(uk.getName(), uk); public Country findCountry(String name) { Assert.notNull(name, "The country's name must not be null"); return countries.get(name);
    package com.example.producingwebservice;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.ws.server.endpoint.annotation.Endpoint;
    import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
    import org.springframework.ws.server.endpoint.annotation.RequestPayload;
    import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
    import io.spring.guides.gs_producing_web_service.GetCountryRequest;
    import io.spring.guides.gs_producing_web_service.GetCountryResponse;
    @Endpoint
    public class CountryEndpoint {
    	private static final String NAMESPACE_URI = "http://spring.io/guides/gs-producing-web-service";
    	private CountryRepository countryRepository;
    	@Autowired
    	public CountryEndpoint(CountryRepository countryRepository) {
    		this.countryRepository = countryRepository;
    	@PayloadRoot(namespace = NAMESPACE_URI, localPart = "getCountryRequest")
    	@ResponsePayload
    	public GetCountryResponse getCountry(@RequestPayload GetCountryRequest request) {
    		GetCountryResponse response = new GetCountryResponse();
    		response.setCountry(countryRepository.findCountry(request.getName()));
    		return response;
       

    The @Endpoint annotation registers the class with Spring WS as a potential candidate for processing incoming SOAP messages.

    The @PayloadRoot annotation is then used by Spring WS to pick the handler method, based on the message’s namespace and localPart.

    The @RequestPayload annotation indicates that the incoming message will be mapped to the method’s request parameter.

    The @ResponsePayload annotation makes Spring WS map the returned value to the response payload.

    package com.example.producingwebservice;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.ClassPathResource;
    import org.springframework.ws.config.annotation.EnableWs;
    import org.springframework.ws.config.annotation.WsConfigurerAdapter;
    import org.springframework.ws.transport.http.MessageDispatcherServlet;
    import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
    import org.springframework.xml.xsd.SimpleXsdSchema;
    import org.springframework.xml.xsd.XsdSchema;
    @EnableWs
    @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 = "countries")
    	public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
    		DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    		wsdl11Definition.setPortTypeName("CountriesPort");
    		wsdl11Definition.setLocationUri("/ws");
    		wsdl11Definition.setTargetNamespace("http://spring.io/guides/gs-producing-web-service");
    		wsdl11Definition.setSchema(countriesSchema);
    		return wsdl11Definition;
    	@Bean
    	public XsdSchema countriesSchema() {
    		return new SimpleXsdSchema(new ClassPathResource("countries.xsd"));
        
  • Spring WS uses a different servlet type for handling SOAP messages: MessageDispatcherServlet. It is important to inject and set ApplicationContext to MessageDispatcherServlet. Without that, Spring WS will not automatically detect Spring beans.

  • Naming this bean messageDispatcherServlet does not replace Spring Boot’s default DispatcherServlet bean.

  • DefaultMethodEndpointAdapter configures the annotation-driven Spring WS programming model. This makes it possible to use the various annotations, such as @Endpoint (mentioned earlier).

  • DefaultWsdl11Definition exposes a standard WSDL 1.1 by using XsdSchema

  • This configuration also uses the WSDL location servlet transformation: servlet.setTransformWsdlLocations(true). If you visit http://localhost:8080/ws/countries.wsdl, the soap:address will have the proper address. If you instead visit the WSDL from the public facing IP address assigned to your machine, you will see that address instead.

    package com.example.producingwebservice;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    @SpringBootApplication
    public class ProducingWebServiceApplication {
    	public static void main(String[] args) {
    		SpringApplication.run(ProducingWebServiceApplication.class, args);
        
  • @Configuration: Tags the class as a source of bean definitions for the application context.

  • @EnableAutoConfiguration: Tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings. For example, if spring-webmvc is on the classpath, this annotation flags the application as a web application and activates key behaviors, such as setting up a DispatcherServlet.

  • @ComponentScan: Tells Spring to look for other components, configurations, and services in the com/example package, letting it find the controllers.

  • The main() method uses Spring Boot’s SpringApplication.run() method to launch an application. Did you notice that there was not a single line of XML? There is no web.xml file, either. This web application is 100% pure Java and you did not have to deal with configuring any plumbing or infrastructure.

    Build an executable JAR

    You can run the application from the command line with Gradle or Maven. You can also build a single executable JAR file that contains all the necessary dependencies, classes, and resources and run that. Building an executable jar makes it easy to ship, version, and deploy the service as an application throughout the development lifecycle, across different environments, and so forth.

    If you use Gradle, you can run the application by using ./gradlew bootRun. Alternatively, you can build the JAR file by using ./gradlew build and then run the JAR file, as follows:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    				  xmlns:gs="http://spring.io/guides/gs-producing-web-service">
       <soapenv:Header/>
       <soapenv:Body>
          <gs:getCountryRequest>
             <gs:name>Spain</gs:name>
          </gs:getCountryRequest>
       </soapenv:Body>
    </soapenv:Envelope>
    # Use inline XML data
    curl <<-EOF -fsSL -H "content-type: text/xml" -d @- http://localhost:8080/ws \
      > target/response.xml && xmllint --format target/response.xml
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
                                      xmlns:gs="http://spring.io/guides/gs-producing-web-service">
       <soapenv:Header/>
       <soapenv:Body>
          <gs:getCountryRequest>
             <gs:name>Spain</gs:name>
          </gs:getCountryRequest>
       </soapenv:Body>
    </soapenv:Envelope>
          
    <?xml version="1.0"?>
    <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
      <SOAP-ENV:Header/>
      <SOAP-ENV:Body>
        <ns2:getCountryResponse xmlns:ns2="http://spring.io/guides/gs-producing-web-service">
          <ns2:country>
            <ns2:name>Spain</ns2:name>
            <ns2:population>46704314</ns2:population>
            <ns2:capital>Madrid</ns2:capital>
            <ns2:currency>EUR</ns2:currency>
          </ns2:country>
        </ns2:getCountryResponse>
      </SOAP-ENV:Body>
    </SOAP-ENV:Envelope>
    Odds are that the output will be a compact XML document instead of the nicely formatted one shown above. If you have xmllib2 installed on your system, you can curl -fsSL --header "content-type: text/xml" -d @request.xml http://localhost:8080/ws > output.xml and xmllint --format output.xml see the results formatted nicely.