简体   繁体   中英

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 ?

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM