1

Spring 4 O/X 在其抽象背后支持多个 XML 解组器。我们使用 JAXB2。

Spring 可以根据模式验证传入的 XML 吗?我在官方文档和描述配置的 spring-oxm模式中都没有找到任何内容。这是我目前的配置,很标准。

<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>
4

1 回答 1

3

“Spring 可以根据模式验证传入的 XML 吗?”

如果设置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> 
于 2014-09-17T20:26:40.223 回答