Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure PayloadValidatingInterceptor using annotation

I am trying to develop a Spring webservice and followed this tutorial https://spring.io/guides/gs/producing-web-service/

The project structure(and the configuration class names) are same as mentioned in the tutorial. I am trying to do all possible configuration using annotation and want to avoid all xml based configuration. So far I even avoided applicationContext.xml and web.xml by using java configuration. However, now I want to introduce XSD validation as shown in this tutorial: http://stack-over-flow.blogspot.com/2012/03/spring-ws-schema-validation-using.html i.e. by extending the PayloadValidatingInterceptor class.As shown in the tutorial, this custom validator interceptor then needs to be registered using the following xml configuration:

<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>

However, I am not sue how to do the above configuration using annotation. i.e. setting the XSD file to the interceptor. I have tried overriding the "addInterceptor" of the WsConfigurerAdaptor class to register the interceptor. Please let me know if I need to do that or what is the correct way of doing the entire thing using annotations.

like image 626
AC_1985 Avatar asked Sep 27 '22 08:09

AC_1985


1 Answers

I'm working with spring-boot and i was looking for a way to do the same, and i found this:

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

In this example the addInterceptors method is the important, the others 3 are basic to expose a WSDL API.

Maybe it'll be useful for someone else.

like image 180
Julio Villane Avatar answered Sep 30 '22 06:09

Julio Villane