使用许多 XSD 文件配置 Jaxb2Marshaller 不起作用 [JAVA 12]
Configuration of Jaxb2Marshaller with many XSD files doesn't work [JAVA 12]
我正在尝试在 spring 中创建自动 XML 编组验证并重新定义 HttpMessageConverter
。我写了一个脚本,它在 WebMvcConfigurer
转换器列表中添加一个 Jaxb2Marshaller
Bean 作为 MessagConverter
。但是当我使用许多 XSD 文件作为模式时,我的 Jaxb2Marshaller
配置出现问题。 Spring 仅识别与第一个 XSD 文件相关的对象。这是我的编组器代码:
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan("nbf.toto.core.xml");
marshaller.setSchemas(new ClassPathResource("/xsd/file1.xsd"), new ClassPathResource("/xsd/file2.xsd"));
Map<String, Object> map = new HashMap<>();
map.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);
map.put(Marshaller.JAXB_FRAGMENT, true);
marshaller.setMarshallerProperties(map);
return marshaller;
}
有示例代码,如何设置多个xsds作为Schemas进行XML验证?我正在使用 Java 12、Srping 5 和 Jaxb 2.3.2
经过几天的搜索,我发现 Jaxb2Marshaller 不允许 XSD 使用不同的根标签对 XML 进行验证。因此,如果您有两个与不同实体相关的不同 XSD,并且您将这些 XSD 添加到 Jaxb2Marshaller bean 进行验证,Jaxb2Marshaller 将只考虑第一个 XSD 文件。而与第二个XSD相关的XML会抛出异常。
因此,为了我的目标,我创建了一个 XML Marshaller 实用程序,用于将 XML 转换为给定的 XSD Class 并进行验证。在我的控制器中,我在@RequestBody 中收到 XML 作为字符串。在我使用我的 util 将此字符串转换为给定的 XSD class 之后。这是我使用的方法示例:
public static Object convertXmlToClass(final String xmlContent, final Class classe, final String xsdFileName) throws Exception {
final JAXBContext context = JAXBContext.newInstance(classe);
final Unmarshaller unmarshaller = context.createUnmarshaller();
try {
final ClassPathResource xsdFile = new ClassPathResource("/xsd/" + xsdFileName + ".xsd");
final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new StreamSource(xsdFile.getInputStream()));
unmarshaller.setSchema(schema);
} catch (Exception e) {
LOG.error(e.getStackTrace());
throw new Wy6Exception("An Error occured during the XML convertion.");
}
Object result = unmarshaller.unmarshal(new StreamSource(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8))));
return result;
}
我正在尝试在 spring 中创建自动 XML 编组验证并重新定义 HttpMessageConverter
。我写了一个脚本,它在 WebMvcConfigurer
转换器列表中添加一个 Jaxb2Marshaller
Bean 作为 MessagConverter
。但是当我使用许多 XSD 文件作为模式时,我的 Jaxb2Marshaller
配置出现问题。 Spring 仅识别与第一个 XSD 文件相关的对象。这是我的编组器代码:
@Bean
public Jaxb2Marshaller jaxb2Marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setPackagesToScan("nbf.toto.core.xml");
marshaller.setSchemas(new ClassPathResource("/xsd/file1.xsd"), new ClassPathResource("/xsd/file2.xsd"));
Map<String, Object> map = new HashMap<>();
map.put(Marshaller.JAXB_FORMATTED_OUTPUT, true);
map.put(Marshaller.JAXB_FRAGMENT, true);
marshaller.setMarshallerProperties(map);
return marshaller;
}
有示例代码,如何设置多个xsds作为Schemas进行XML验证?我正在使用 Java 12、Srping 5 和 Jaxb 2.3.2
经过几天的搜索,我发现 Jaxb2Marshaller 不允许 XSD 使用不同的根标签对 XML 进行验证。因此,如果您有两个与不同实体相关的不同 XSD,并且您将这些 XSD 添加到 Jaxb2Marshaller bean 进行验证,Jaxb2Marshaller 将只考虑第一个 XSD 文件。而与第二个XSD相关的XML会抛出异常。
因此,为了我的目标,我创建了一个 XML Marshaller 实用程序,用于将 XML 转换为给定的 XSD Class 并进行验证。在我的控制器中,我在@RequestBody 中收到 XML 作为字符串。在我使用我的 util 将此字符串转换为给定的 XSD class 之后。这是我使用的方法示例:
public static Object convertXmlToClass(final String xmlContent, final Class classe, final String xsdFileName) throws Exception {
final JAXBContext context = JAXBContext.newInstance(classe);
final Unmarshaller unmarshaller = context.createUnmarshaller();
try {
final ClassPathResource xsdFile = new ClassPathResource("/xsd/" + xsdFileName + ".xsd");
final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
Schema schema = sf.newSchema(new StreamSource(xsdFile.getInputStream()));
unmarshaller.setSchema(schema);
} catch (Exception e) {
LOG.error(e.getStackTrace());
throw new Wy6Exception("An Error occured during the XML convertion.");
}
Object result = unmarshaller.unmarshal(new StreamSource(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8))));
return result;
}