Java – Spring Ws – 在XSD文件中加载相对包含(Tomcat 8)

我创建了一个Spring Web服务,它使用以下代码从XSD文件集合创建动态WSDL:

Resource[] schema = { new ClassPathResource( "schema/service/XCPD.SupportMaterials.v9/schema/HL7V3/NE2008/coreschemas/NarrativeBlock.xsd"), new ClassPathResource( "schema/service/XCPD.SupportMaterials.v9/schema/HL7V3/NE2008/coreschemas/datatypes-base.xsd"), new ClassPathResource( "schema/service/XCPD.SupportMaterials.v9/schema/HL7V3/NE2008/coreschemas/infrastructureRoot.xsd"), new ClassPathResource( "schema/service/XCPD.SupportMaterials.v9/schema/HL7V3/NE2008/multicacheschemas/PRPA_IN201305UV02.xsd"), new ClassPathResource( "schema/service/XCPD.SupportMaterials.v9/schema/HL7V3/NE2008/multicacheschemas/PRPA_IN201306UV02.xsd"), new ClassPathResource( "schema/service/XCPD.SupportMaterials.v9/schema/IHE/XCPD_PLQ.xsd"), new ClassPathResource( "schema/service/XCPD.SupportMaterials.v9/schema/HL7V3/XCPD_PRPA.xsd") }; CommonsXsdSchemaCollection collection = new CommonsXsdSchemaCollection( schema); collection.setInline(true); return collection; 

用于创建动态WSDL的XSD文件包括使用include语句的其他各种模式文件,如下所示:

   

当我在Tomcat 8容器中运行代码时,我收到以下exception:

 Caused by: java.lang.IllegalArgumentException: The resource path [/../coreschemas/infrastructureRoot.xsd] has been normalized to [null] which is not valid 

Spring的URI解析器在路径前面加上“/”,即使被引用的文件是相对路径(不是绝对路径),并且在导入模式时失败。

应该注意的是,这个应用程序在Tomcat 7上运行正常。当尝试将其迁移到Tomcat 8时,问题就出现了。

Tomcat 8确实改变了现在加载Web资源的方式。 来自Java CodeRanch的更多信息 ……

长话短说,有没有办法强制Spring,URI解析器应该正确处理相关文件? 如果我查看解析器Spring使用的“collectionBaseURI”属性(ClasspathUriResolver),则此值为null。 有没有办法设置这个基本URI?

编辑我可以通过将所有相对路径转换为模式的绝对路径来解决此问题…但是我不想在数百个文件中应用此修复。

如果有人遇到这个恼人的问题, ClasspathUriResolver是罪魁祸首,在相对路径包括前面加上“/”。 我将其切换为使用默认URI解析器(在org.apache.ws.commons.schema.resolver.DefaultURIResolver ),它在Tomcat 8上运行正常,没有问题。

 CommonsXsdSchemaCollection collection = new CommonsXsdSchemaCollection(schema); collection.setUriResolver(new DefaultURIResolver()); collection.setInline(true); return collection;