Spring 4 O/X 在其抽象背后支持多个 XML 解码器。我们使用 JAXB2。
Spring 能否根据模式验证传入的 XML?我在官方documentation里面没查到什么也不在 spring-oxm schema描述配置的。这是我目前的配置,非常标准。
<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="marshallerProperties">
<map>
<!-- properties here -->
</map>
</property>
<property name="classesToBeBound">
<list>
<value>com.example.Message1</value>
<value>com.example.Message2</value>
</list>
</property>
</bean>
最佳答案
"Can Spring validate the incoming XML against a schema?"
如果设置schema
属性:
public class Jaxb2Marshaller ... {
/**
* Set the schema resource to use for validation.
*/
public void setSchema(Resource schemaResource) {
this.schemaResources = new Resource[] {schemaResource};
}
/**
* Set the schema resources to use for validation.
*/
public void setSchemas(Resource... schemaResources) {
this.schemaResources = schemaResources;
}
}
Jaxb2Marshaller
将使用这些模式进行验证。所以在你的上下文 xml 中,你可以做类似的事情
<bean id="jaxb2Marshaller"
class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="schema" value="classpath:myschema.xsd"/>
<property name="classesToBeBound">
<list>
<value>com.example.Message1</value>
<value>com.example.Message2</value>
</list>
</property>
</bean>
关于java - 配置 Spring 4.1 O/X 映射以基于模式验证入站 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25897274/