如何根据 WSDL 打开验证 - spring boot 和 spring-ws

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

在我的架构中,我有以下元素:

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

我有它的端点:

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

当我在此端点发送时:

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

已接受,但应在验证阶段拒绝。为什么 ?因为我设置了 minOccurs=1,但它接受了带有 0 个元素的信封。
如何根据 WSDL 打开验证?

配置验证拦截器。

xml 配置

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

或使用 java 配置

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