我探索JAXB和JAXB2 Maven插件。我对bean实例感到困惑。
我在src/main/resources
中有两个具有相同命名空间www.mycompany.com
的架构。 schemaA.xsd 和 schemaB.xsd 具有相同的标头:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:tns="http://www.mycompany.com"
targetNamespace="http://www.mycompany.com"
elementFormDefault="qualified">
我的WebServiceConfiguration
扩展WsConfigurerAdapter
定义了ServletRegistrationBean
以及以下XsdSchema(Collection)
豆:
// Just a bean with one schema
@Bean
public XsdSchema schema() {
return new SimpleXsdSchema(new ClassPathResource("xsd/schemaA.xsd"));
}
// A bean with both schemas
@Bean
public XsdSchemaCollection schemaCollection() {
return new XsdSchemaCollection() {
@Override
public XsdSchema[] getXsdSchemas() {
return new XsdSchema[] {
new SimpleXsdSchema(new ClassPathResource("xsd/schemaA.xsd")),
new SimpleXsdSchema(new ClassPathResource("xsd/schemaB.xsd"))
};
}
@Override
public XmlValidator createValidator() {
throw new UnsupportedOperationException();
}
};
}
还有WSDL bean:
@Bean(name = "soapWsdl")
public DefaultWsdl11Definition defaultWsdl11Definition(XsdSchema schema, XsdSchemaCollection schemaCollection) {
DefaultWsdl11Definition wsdl11Definition = new DefaultWsdl11Definition();
wsdl11Definition.setPortTypeName("SoapPort");
wsdl11Definition.setLocationUri("/service/soap");
wsdl11Definition.setTargetNamespace("https://www.mycompany.com");
// At this point I use just one of the folliwing
wsdl11Definition.setSchema(schema);
wsdl11Definition.setSchemaCollection(schemaCollection);
return wsdl11Definition;
}
在上面的bean中使用其中之一:
wsdl11Definition.setSchema(schema);
如果仅使用XsdSchema
定义一个模式(无论schemaA还是schemaB),一切正常,则SOAP Web服务正在运行,并且生成了WSDL
wsdl11Definition.setSchemaCollection(schemaCollection);
现在,尽管InliningXsdSchemaTypesProvider
将一个模式设置为具有一个模式的集合,但将引发NPE。引发的错误是:
2018-10-05 21:29:28.636 WARN 164968 --- [main] ConfigServletWebServerApplicationContext:上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.BeanCreationException:创建名称为'soapWsdl的bean时出错'在类路径资源[cz / vse / soap / WebServiceConfig.class]中定义:通过工厂方法实例化Bean失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition]:工厂方法“ defaultWsdl11Definition”引发了异常;嵌套的异常是java.lang.NullPointerException
以及以下几点:
原因:java.lang.NullPointerException:null 在org.springframework.xml.xsd.SimpleXsdSchema.getTargetNamespace(SimpleXsdSchema.java:94)〜[spring-xml-3.0.3.RELEASE.jar!/:na] 在com.mycompany.WebServiceConfig.defaultWsdl11Definition(WebServiceConfig.java:66)〜[classes!/:0.0.1]
我通过调用SimpleXsdSchema::afterPropertiesSet
使事情正常进行,在某种程度上,bean实例化没有调用它。很难说会发生什么。但是,像这样编辑方法会使架构协同工作:
@Override
public XsdSchema[] getXsdSchemas() {
SimpleXsdSchema[] schemaArray = new SimpleXsdSchema[] {
new SimpleXsdSchema(new ClassPathResource("xsd/schemaA.xsd")),
new SimpleXsdSchema(new ClassPathResource("xsd/schemaB.xsd"))
};
for (SimpleXsdSchema schema : schemaArray) {
try {
schema.afterPropertiesSet();
} catch (ParserConfigurationException | IOException | SAXException e) { /* ...*/}
}
return schemaArray;
}
有人知道会发生什么吗?为什么通过XsdSchema
和XsdSchemaCollection
即使他们应该按照DefaultWsdl11Definition
进行操作,行为也不一样?