2017-01-25 71 views
1

我已经创建了一个简单的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') 
} 

回答

0

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