spring – 如何使用注释配置PayloadValidatingInterceptor

我正在尝试开发
Spring Web服务并遵循本教程
https://spring.io/guides/gs/producing-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>

但是,我没有起诉如何使用注释进行上述配置.即将XSD文件设置为拦截器.我试过覆盖WsConfigurerAdaptor类的“addInterceptor”来注册拦截器.如果我需要这样做,或者使用注释完成整个事情的正确方法,请告诉我.

最佳答案 我正在使用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"));
  }
}

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

也许这对其他人有用.

点赞