繁体   English   中英

为什么解组器行为不一致?

[英]Why Unmarshaller behaviour is not consistent?

我创建了一个简单的 web 服务 (spring-web),用于验证传入的 XML 与 XSD 的相关性,如果发生任何验证错误,它们应该返回给请求者。

下面的代码完成它的工作......

@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 个错误。 同样,对于第二个和第三个请求。 但是,在第 4 次请求时,UnmarshallingContext 中的计数器达到零(从 10 开始计数)并返回以下错误:

Errors limit exceeded. To receive all errors set com.sun.xml.internal.bind logger to FINEST level.

在第五次请求时,它不会抛出任何验证错误! 只是为了说明这一点 - 所有请求都完全相同。

为什么 UnmarhsallingContext 有一个静态计数器可以阻止第 5 个以上的请求被验证? 我怎样才能克服这个问题?

我的构建配置: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')
}

我今天遇到了同样的错误,并通过应用 JAXB 的Messages.properties 中的建议解决了它:添加java.util.logging.Logger.getLogger("com.sun.xml.internal.bind").setLevel(Level.FINEST); 在调用解组之前。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM