【问题标题】:How to switch on validation according to WSDL - spring boot and spring-ws如何根据 WSDL 开启验证——spring boot 和 spring-ws
【发布时间】:2017-09-02 19:37:30
【问题描述】:

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

<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 开启验证?

【问题讨论】:

    标签: java web-services spring-boot soap spring-ws


    【解决方案1】:

    配置一个验证拦截器。

    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
    }
    

    【讨论】:

    • @HaskellFun PayloadValidatingInterceptor 还具有用于模式列表的 setXsdSchemaCollection() 方法
    • 我有很多文件的问题。你能附上许多 xsd 文件的例子吗?
    • 你使用了@Override 你覆盖了什么?
    • 配置类扩展WsConfigurerAdapter.
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 2017-09-11
    • 2015-01-30
    • 1970-01-01
    相关资源
    最近更新 更多