我已經創建了一個簡單的web服務(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 +個請求?我怎樣才能克服這個問題?
我的生成配置:春天開機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')
}