end-user
end-user

Reputation: 2947

PayloadValidatingInterceptor not firing for @Endpoint?

I have an application that I would like to automatically validate messages that are received and sent. I've attached the PayloadValidatingInterceptor and set the schema I would like it to use:

@EnableWs
@Configuration
public class WebServiceConfig extends WsConfigurerAdapter {
    @Autowired
    private ApplicationContext ctx;

    @Bean
    public ServletRegistrationBean messageDispatcherServlet(ApplicationContext applicationContext) {
        MessageDispatcherServlet servlet = new MessageDispatcherServlet();
        servlet.setApplicationContext(applicationContext);
        // modifies the wsdl to serve the correct locations
        servlet.setTransformWsdlLocations(true);
        return new ServletRegistrationBean(servlet, "/*");
    }

    @Bean
    protected PayloadValidatingInterceptor getValidatingInterceptor() {
        PayloadValidatingInterceptor validatingInterceptor = new PayloadValidatingInterceptor();
        validatingInterceptor.setSchema(getResource("classpath:CARetriever.xsd"));
        validatingInterceptor.setValidateRequest(true);
        validatingInterceptor.setValidateResponse(true);
        return validatingInterceptor;
    }

    private Resource getResource(String resource) {
        return ctx.getResource(resource);
    }
}

I can see that the interceptor is getting loaded

2016-01-21 14:22:08 INFO  org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor,164 - Validating using class path resource [CARetriever.xsd]

However, when I throw an invalid SOAP message against it, I get a NullPointerException rather than a validation message. So, either my configuration or expectations are wrong. Can someone point to which?

Upvotes: 2

Views: 1819

Answers (1)

beat
beat

Reputation: 2022

I got the same error: a SOAPFault with a NPE inside with no stack trace. While debugging I noticed that the validator instance used in PayloadValidatingInterceptor is null, because setSchema does not initialize it. But there is the method setXsdSchema, which does initialize it. So, I could fix the NPE using that method instead of setSchema as follows:

payloadValidatingInterceptor.setXsdSchema(new SimpleXsdSchema(new ClassPathResource("test.xsd")));

For a complete example one may check this example: http://memorynotfound.com/spring-ws-validate-soap-request-response-xsd-schema/

Hope that helps.

Upvotes: 1

Related Questions