找不到元素'constraint-mappings'的声明

时间:2018-10-23 11:15:16

标签: xml bean-validation

阅读文档: https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-mapping-xml-constraints

为了简洁起见,我删除了一个长bean定义,然后其源代码如下所示。执行后,我得到:Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'constraint-mappings'.好,所以架构或xml文件应该有问题。

但是。我从来没有真正理解过所有奇怪的命名空间定义。因此,前3个http URL无处可去,它们可能有更高的用途,而以xsd结尾的最后一个确实存在并且确实定义了constraint-mappings元素。我相信整个文件都是有效的,据Intellij IDEA所述。如果我尝试破坏它,则IntellijIDEA会看到它。因此,我认为它实际上是有效的。因此,为什么在地球上弹簧在启动时会产生给定的错误?

validation.xml文件:

<constraint-mappings
    xmlns="http://xmlns.jcp.org/xml/ns/validation/mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/mapping
            http://xmlns.jcp.org/xml/ns/validation/mapping/validation-mapping-2.0.xsd"
    version="2.0">

  <default-package>org.hibernate.validator.referenceguide.chapter05</default-package>
  <bean class="RentalCar">
    <class ignore-annotations="true">
      <group-sequence>
        <value>RentalCar</value>
        <value>CarChecks</value>
      </group-sequence>
    </class>
  </bean>
  <constraint-definition annotation="org.mycompany.CheckCase">
    <validated-by include-existing-validators="false">
      <value>org.mycompany.CheckCaseValidator</value>
    </validated-by>
  </constraint-definition>
</constraint-mappings>

1 个答案:

答案 0 :(得分:0)

validation.xml文件专用于提供验证工厂的配置。您可以配置时钟提供程序,其他值提取器等。也可以在其中引用约束映射文件(您的帖子中有一个)。这是documentation的示例的一部分:

<validation-config
        xmlns="http://xmlns.jcp.org/xml/ns/validation/configuration"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/validation/configuration
            http://xmlns.jcp.org/xml/ns/validation/configuration/validation-configuration-2.0.xsd"
        version="2.0">

    <default-provider>com.acme.ValidationProvider</default-provider>

    <message-interpolator>com.acme.MessageInterpolator</message-interpolator>
    <traversable-resolver>com.acme.TraversableResolver</traversable-resolver>
    <constraint-validator-factory>
        com.acme.ConstraintValidatorFactory
    </constraint-validator-factory>
    <parameter-name-provider>com.acme.ParameterNameProvider</parameter-name-provider>
    <clock-provider>com.acme.ClockProvider</clock-provider>

    <value-extractor>com.acme.ContainerValueExtractor</value-extractor>

    <constraint-mapping>META-INF/validation/constraints-car.xml</constraint-mapping>

    <property name="hibernate.validator.fail_fast">false</property>
</validation-config>

因此,validation.xml应该具有validation-config元素。这很可能是您收到该错误的原因。