Search code examples
javaweb-servicesspring-bootsoapspring-ws

How to switch on validation according to WSDL - spring boot and spring-ws


in my schema I have following element:

<xs:element name="deletePokemonsRequest">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="pokemonId" type="xs:int" minOccurs="1" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

And I have endpoint for it:

@PayloadRoot(namespace = NAMESPACE_URI, localPart = "deletePokemonsRequest")
@ResponsePayload
public DeletePokemonsRequest deletePokemons(@RequestPayload DeletePokemonsRequest deletePokemons){
    pokemonDAO.deletePokemons(deletePokemons.getPokemonId());
    return deletePokemons;
}

When I send on this endpoint:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pok="www">
   <soapenv:Header/>
   <soapenv:Body>
      <pok:deletePokemonsRequest>               
      </pok:deletePokemonsRequest>
   </soapenv:Body>
</soapenv:Envelope>

It is accepted, but it should be rejected on validation stage. Why ? Because I set minOccurs=1, but it accepted envelope with 0 elements.
How to turn on validation according to WSDL ?


Solution

  • Configure a validating interceptor.

    xml config

    <bean id="validatingInterceptor" class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor">
        <property name="xsdSchema" ref="schema" />
        <property name="validateRequest" value="true" />
        <property name="validateResponse" value="true" />
    </bean>
    <bean id="schema" class="org.springframework.xml.xsd.SimpleXsdSchema">
        <property name="xsd" value="your.xsd" />
    </bean>
    

    or with java config

    @Configuration
    @EnableWs
    public class MyWsConfig extends WsConfigurerAdapter {
    
        @Override
        public void addInterceptors(List<EndpointInterceptor> interceptors) {
            PayloadValidatingInterceptor validatingInterceptor = new PayloadValidatingInterceptor();
            validatingInterceptor.setValidateRequest(true);
            validatingInterceptor.setValidateResponse(true);
            validatingInterceptor.setXsdSchema(yourSchema());
            interceptors.add(validatingInterceptor);
        }
    
        @Bean
        public XsdSchema yourSchema(){
            return new SimpleXsdSchema(new ClassPathResource("your.xsd"));
        }
        // snip other stuff
    }