XSDに関して入力XMLを検証するための単純なWebサービス(spring-web)を作成しました。検証エラーが発生した場合は、要求者に返す必要があります。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 */
}
その仕事をしていません...しかし、奇妙な理由のために、それが処理される要求の間の誤差を「累積」です。 1回目の要求中に3つのエラーが返されます。同様に、2番目と3番目のリクエスト。ただし、第四の要求に、UnmarshallingContextでカウンタがゼロ(10からカウントダウン)に達すると、次のエラーが返されます。第五リクエストで
Errors limit exceeded. To receive all errors set com.sun.xml.internal.bind logger to FINEST level.
は、それが検証エラーをスローしません!それを明示するために、すべての要求はまったく同じです。
なぜUnmarhsallingContextに5番目の要求を検証するのを停止するスタティックカウンタがあるのですか?どうすればこの問題を克服できますか?
私のビルド設定:春のブート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')
}