生成SOAP Web服务示例会引发错误

时间:2016-01-20 21:11:19

标签: java spring web-services soap xsd

这是我的XML架构

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

和我的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"));
    }
}

所以每次我想执行我的程序时都会出现错误读取

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

我不知道为什么......

此处还有示例代码的链接(其余部分) https://spring.io/guides/gs/convert-jar-to-war/

1 个答案:

答案 0 :(得分:1)

您的代码失败,因为在类路径的根目录找不到countries.xsd 。您需要在资源的类路径中指定绝对路径,或者将countries.xsd移动到类路径的根目录。