如何使用注释配置PayloadValidatingInterceptor

AC_*_*985 5 spring spring-ws spring-boot

我正在尝试开发Spring Web服务并遵循本教程https://spring.io/guides/gs/production-web-service/

项目结构(和配置类名称)与本教程中提到的相同。我正在尝试使用注释进行所有可能的配置,并希望避免所有基于xml的配置。到目前为止,我什至通过使用Java配置避免了applicationContext.xml和web.xml。但是,现在,我想介绍本教程中所示的XSD验证:http : //stack-over-flow.blogspot.com/2012/03/spring-ws-schema-validation-using.html,即通过扩展PayloadValidatingInterceptor类如本教程所示,此自定义验证器拦截器随后需要使用以下xml配置进行注册:

<bean class="org.springframework.ws.server.endpoint.mapping.PayloadRootAnnotationMethodEndpointMapping">
 <property name="interceptors">
  <list>
    <ref bean="validatingInterceptor"/>   
  </list>
 </property>
</bean>

<bean id="validatingInterceptor" class="com.test.ValidationInterceptor ">
     <property name="schema" value="/jaxb/test.xsd"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

但是,我不起诉如何使用注释进行上述配置。即将XSD文件设置为拦截器。我尝试覆盖WsConfigurerAdaptor类的“ addInterceptor”以注册拦截器。请让我知道是否需要这样做,或者使用批注完成整件事的正确方法是什么。

Jul*_*ane 5

我正在使用 spring-boot,我一直在寻找一种方法来做同样的事情,我发现了这个:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {

  @Override
  public void addInterceptors(List<EndpointInterceptor> interceptors) {
    PayloadValidatingInterceptor validatingInterceptor = new PayloadValidatingInterceptor();
    validatingInterceptor.setValidateRequest(true);
    validatingInterceptor.setValidateResponse(true);
    validatingInterceptor.setXsdSchema(resourceSchema());
    interceptors.add(validatingInterceptor);
  }

  @Bean
  public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
    MessageDispatcherServlet servlet = new MessageDispatcherServlet();
    servlet.setApplicationContext(applicationContext);
    servlet.setTransformWsdlLocations(true);
    return new ServletRegistrationBean(servlet, "/api/*");
  }

  @Bean(name = "registros")
  public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema countriesSchema) {
    DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
    wsdl11Definition.setPortTypeName("ResourcePort");
    wsdl11Definition.setLocationUri("/api");
    wsdl11Definition.setTargetNamespace("http://resource.com/schema");
    wsdl11Definition.setSchema(resourceSchema());
    return wsdl11Definition;
  }

  @Bean
  public XsdSchema resourceSchema() {
    return new SimpleXsdSchema(new ClassPathResource("registro.xsd"));
  }
}
Run Code Online (Sandbox Code Playgroud)

在此示例中,addInterceptors方法很重要,其他 3 个方法是公开 WSDL API 的基本方法。

也许对其他人有用。


AC_*_*985 1

我可以使用以下方法解决它:

@Override
public void addInterceptors(List<EndpointInterceptor> interceptors) {
    interceptors.add(validationInterceptor());
}
@Bean
ValidationInterceptor validationInterceptor() {
        final ValidationInterceptor payloadValidatingInterceptor = new ValidationInterceptor();
        payloadValidatingInterceptor.setSchema(new ClassPathResource(
                "XSD/Pay.xsd"));
        return payloadValidatingInterceptor;
    }
Run Code Online (Sandbox Code Playgroud)

然后我必须使用以下命令从验证器类中读取架构文件

SchemaFactory schemaFactory = SchemaFactory.newInstance(getSchemaLanguage());
        Schema schema = schemaFactory.newSchema(getSchemas()[0].getURL());
        Validator validator = schema.newValidator();
Run Code Online (Sandbox Code Playgroud)