如何在验证根据WSDL切换 - 春天引导和弹簧WS
问题描述:
在我的模式我有以下元素:如何在验证根据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打开验证?
答
配置验证拦截器。
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
}
怎么样在许多xsd文件的情况下? –
@HaskellFun PayloadValidatingInterceptor也有setXsdSchemaCollection()方法列表模式 – eis
我有许多文件的问题。你能附上很多xsd文件的例子吗? –