I have created a simple webservice (spring-web) which is meant to validate the incoming XML with respect to XSD and if any validation errors occured, they should be returned to the requester.
The below code does its job...
@PostMapping(path = "/test", consumes = MediaType.APPLICATION_XML_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Object> method2(@RequestBody Source request) throws IOException, JAXBException, SAXException {
final URL schemaUrl = resourceLoader.getResource("classpath:test.xsd").getURL();
final CumulatingErrorHandler errorHandler = new CumulatingErrorHandler();
final Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
.newSchema(schemaUrl);
final ValidationEventCollector validationEventCollector = new MyValidationEventCollector();
final Unmarshaller unmarshaller = JAXBContext
.newInstance(IncomingRequestModel.class)
.createUnmarshaller();
unmarshaller.setEventHandler(validationEventCollector);
unmarshaller.setSchema(schema);
final IncomingRequestModel requestModel = (IncomingRequestModel) unmarshaller.unmarshal(request);
if (validationEventCollector.hasEvents()) {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8);
return new ResponseEntity<>(validationEventCollector.getEvents(), responseHeaders, HttpStatus.BAD_REQUEST);
}
/* do stuff */
}
... but, for a weird reason, it's 'cumulating' the errors between the requests handled. During the 1st request, 3 errors are returned. Similarly, for the 2nd and 3rd requests. However, on 4th request, the counter in UnmarshallingContext reaches zero (counting down from 10) and the following error is returned:
Errors limit exceeded. To receive all errors set com.sun.xml.internal.bind logger to FINEST level.
On fifth request, it doesn't throw any validation errors! Just to make it evident - all requests are exactly the same.
Why the UnmarhsallingContext has a static counter that stops the 5+th requests to be validated? How can I overcome this issue?
My build config: Spring boot 1.4.3.RELEASE, gradle deps:
dependencies {
compile('org.springframework.boot:spring-boot-starter-amqp')
compile('org.springframework.boot:spring-boot-starter-data-redis')
compile('org.springframework.boot:spring-boot-starter-jersey')
compile('org.springframework.boot:spring-boot-starter-web')
compile("com.fasterxml.jackson.core:jackson-databind")
compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")
compileOnly('org.projectlombok:lombok:1.16.12')
compile("net.sf.dozer:dozer:5.5.1")
compile("net.sf.dozer:dozer-spring:5.5.1")
compile("org.apache.commons:commons-lang3:3.5")
testCompile('org.springframework.boot:spring-boot-starter-test')
}