2017-09-02 58 views
2

在我的模式我有以下元素:如何在驗證根據WSDL切換 - 春天引導和彈簧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打開驗證?

回答

4

配置驗證攔截器。

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 
} 
+0

怎麼樣在許多xsd文件的情況下? –

+0

@HaskellFun PayloadValidatingInterceptor也有setXsdSchemaCollection()方法列表模式 – eis

+0

我有許多文件的問題。你能附上很多xsd文件的例子嗎? –