Sand Man
Sand Man

Reputation: 85

Producing a SOAP web service example throws an error

Here is my XML schema

<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>

and my EndPoint

package hello;

import org.springframework.boot.context.embedded.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(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"));
    }
}

So each time I want to execute my program I get an error reading

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'countries' defined in class path resource [hello/WebServiceConfig.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.xml.xsd.XsdSchema]: : Error creating bean with name 'countriesSchema' defined in class path resource [hello/WebServiceConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: xsd 'class path resource [countries.xsd]' does not exist; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'countriesSchema' defined in class path resource [hello/WebServiceConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: xsd 'class path resource [countries.xsd]' does not exist

and I have no idea why...

Also here is the link to the example code (The rest of it) https://spring.io/guides/gs/convert-jar-to-war/

Upvotes: 1

Views: 4152

Answers (1)

N&#225;ndor Előd Fekete
N&#225;ndor Előd Fekete

Reputation: 7098

Your code is failing because countries.xsd cannot be found at the root of your classpath. You need to specify the absolute path in the classpath to your resource OR move your countries.xsd to the root of the classpath.

Upvotes: 2

Related Questions