1

everyone.

So, I have a SpringBoot application with a controller that has several methods, taking the following POJO as a parameter:

package com.example.dto;

import lombok.Data;

@Data
public class MyEntity {
   @NotNull
   private String fieldA;

   private String fieldB;
}

For one of the controller endpoints I would like to apply additional validation logic, so in the validation.xml I add the following:

<constraint-mappings>
    <bean class="com.example.controller.SampleController" ignore-annotations="false">
        <method name="doFoo">
            <parameter type="com.example.dto.MyEntity">
                <valid />
                <constraint annotation="com.example.validation.ValidEntity" />
            </parameter>
        </method>
    </bean>
</constraint-mappings>

com.example.validation.ValidEntity is the constraint annotation I would like to apply.

My problem is that this additional constraint is only invoked if @NotNull checks defined in MyEntity have passed successfully. If fieldA is null, ValidEntity constraint is ignored, and the client receives an imcomplete validation result. What am I missing?

Alex Kiselev
  • 552
  • 2
  • 7
  • 22

2 Answers2

0

I'm not entirely sure about this because I've never worked with the validation.xml file.

However, I would say that Spring is first creating the object and then applying the validations. The @NotNull validation is performed in the creation of the instance. This means that if that validation fails the construction will throw an exception and Spring won't even try to check your constraint (which makes sense in my opinion).

I think you can "fix" it by creating an annotation with your constraint and using it in your class. If I'm right, both annotations will be checked and the thrown exception will contain all errors.

It's just a guess. Let me know if it works.

Zevesh
  • 81
  • 1
  • 9
0

I don't know if there is an easy way to configure the validator to aggregate constraint violations from both annotation and XML configurations when first or both fails.

As demonstrated by your code Hibernate Validator can work with mixed annotation and XML configurations, but the lack of documentation for that specific case is a hint that it is at least not recommended.

When XML configuration file is used, it takes precedence over annotations by default. ignore-annotations is used to overcome this (text highlight is mine):

Setting ignore-annotations to true means that constraint annotations placed on the configured bean are ignored. The default for this value is true. ignore-annotations is also available for the nodes class, fields, getter, constructor, method, parameter, cross-parameter and return-value. If not explicitly specified on these levels the configured bean value applies.

Using Hibernate Validator to Cover Your Validation Needs article states that:

The default for a field is ignore-annotations=”false”. This means that by default annotations for a field are stronger (this is of course after you indicated that that the bean itself wont ignore annotations). If you wont that the XML will be stronger than you have to indicate that by ignore-annotations=”true”

It seems possible to disable annotation configuration for a specific field which is configured in XML.

Another solution to switch between annotation and XML configuration is to use Grouping constraints.

I'm not sure if anything of the above is of any use for you, but if it is possible I would probably switch to a single configuration (XML, assuming that annotation config comes from external library you cannot modify) and enforce it everywhere instead of relying on undocumented features.

MartinBG
  • 1,500
  • 13
  • 22