我正在尝试开发 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"));
}
}
在这个例子中 添加拦截器 方法很重要,其他 3 个是公开 WSDL API 的基础。
也许它对其他人有用。
关于spring - 如何使用注解配置 PayloadValidatingInterceptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33394647/