簡體   English   中英

如何根據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
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM