我正在尝试开发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的基础。

也许对其他人有用。

09-11 20:22