Я создал простой веб-сервис (spring-web), который предназначен для проверки входящего XML в отношении XSD и, если возникли какие-либо ошибки проверки, они должны быть возвращены запрашивающему.Почему поведение Unmarshaller несовместимо?
Код ниже делает свою работу ...
@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 */
}
... но, по странной причине, это «сложение» ошибки между запросами обрабатываются. Во время первого запроса возвращается 3 ошибки. Аналогично, для 2-го и 3-го запросов. Тем не менее, на 4-запросе, счетчик в UnmarshallingContext достигает нуля (обратный отсчет от 10) и возвращается следующее сообщение об ошибке:
Errors limit exceeded. To receive all errors set com.sun.xml.internal.bind logger to FINEST level.
На пятом запросе, не бросайте проверки ошибок! Просто, чтобы сделать это очевидным - все запросы точно такие же.
Почему у UnmarhsallingContext есть статический счетчик, который останавливает проверку 5 + th запросов? Как я могу решить эту проблему?
Моя сборка конфигурация: Spring загрузка 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')
}