Publishing Static WSDL and related XSD schemas usi

2019-06-18 20:18发布

问题:

I have one module where I have my XSD schemas, where one schema can reference an other using relative path within schema location:

<xs:import namespace="http://my.namespace.org" schemaLocation="../mypackage/my.xsd"/>

Here I'm also using xjc to generate Jaxb beans from these xsd schemas.

Now I have a module where is my web-service is implemetented, using spring-ws (2.0.4). And I want to use static WSDL and publish it with the xsd schemas, where the schema locations will be transformed to URL like 'http://myerver.url.com/my.xsd'.

The question is how to elegantly achieve this?

(Alternatively combine the XSDs into one schema and that to WSDL)

(In theory I could transform these XSD using a script and add them to resources (xsd and wsdl) to a (spring dispatcher) servlet but it doesn't seems to me very comfortable)

回答1:

Spring web service has a way to do it elegantly actually. All you need to do is to define a SimpleXsdSchema bean with the right id (which will be used as the xsd name without .xsd) in the bean definition xml file, something like below

<bean id="my" 
        class="org.springframework.xml.xsd.SimpleXsdSchema">
        <property 
            name="xsd" 
            value="/mypackage/my.xsd">
        </property>
</bean>

More information (including an example) can be found at the following link: Static WSDL with imported XML Schema in Spring Web Service



回答2:

Below is the JAva config for exposing the schema. This worked for me. Please note that the schema name should match the Bean name and method name. This is very key for this to work. So i kept the XSD name and Bean name as "CustomerDetailsSchema" and make sure the constructor for getCustomerDetails also matches the name

@Bean(name = "customerDetails")
public DefaultWsdl11Definition getCustomerDetails(XsdSchema CustomerDetailsSchema) {
    DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    wsdl11Definition.setPortTypeName("...");
    wsdl11Definition.setServiceName("...");
    wsdl11Definition.setLocationUri("/webservice");
    wsdl11Definition.setTargetNamespace("...");
    wsdl11Definition.setSchema(CustomerDetailsSchema);
    return wsdl11Definition;
}

@Bean(name = "CustomerDetailsSchema")
public XsdSchema CustomerDetailsSchema() {
    return new SimpleXsdSchema(new ClassPathResource("schemas/CustomerDetailsSchema.xsd"));
}