2015-10-28 102 views
2

我想開發一個web服務春季配置PayloadValidatingInterceptor並遵循本教程https://spring.io/guides/gs/producing-web-service/如何使用註釋

項目結構(與配置類的名字),如提及教程相同。 我想要做所有可能的配置使用註釋,並希望避免所有基於xml的配置。到目前爲止,我甚至通過使用java配置來避免applicationContext.xml和web.xml。不過,現在我要介紹的XSD驗證如本教程:

<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> 
http://stack-over-flow.blogspot.com/2012/03/spring-ws-schema-validation-using.html通過擴展在本教學中PayloadValidatingInterceptor class.As即該自定義驗證攔截器則需要使用下面的xml配置註冊

但是,我不是起訴如何使用註釋來完成上述配置。即將XSD文件設置爲攔截器。我嘗試覆蓋WsConfigurerAdaptor類的「addInterceptor」來註冊攔截器。請讓我知道我是否需要這樣做,或者使用註釋做整個事情的正確方法是什麼。

回答

0

我可以解決這個問題使用下列內容:

@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; 
    } 

,然後我不得不使用下面的

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

來讀取驗證類架構文件我與春天的引導工作,我一直在尋找一種方式做同樣的,我發現這一點:

@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的基礎。

也許它會對別人有用。