Has*_*Fun 3 java soap web-services spring-ws spring-boot
在我的架构中,我具有以下元素:
<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>
Run Code Online (Sandbox Code Playgroud)
我有终点:
@PayloadRoot(namespace = NAMESPACE_URI, localPart = "deletePokemonsRequest")
@ResponsePayload
public DeletePokemonsRequest deletePokemons(@RequestPayload DeletePokemonsRequest deletePokemons){
pokemonDAO.deletePokemons(deletePokemons.getPokemonId());
return deletePokemons;
}
Run Code Online (Sandbox Code Playgroud)
当我发送此端点时:
<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>
Run Code Online (Sandbox Code Playgroud)
它被接受,但在验证阶段应被拒绝。为什么呢 因为我设置了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>
Run Code Online (Sandbox Code Playgroud)
或与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
}
Run Code Online (Sandbox Code Playgroud)