Note that there are some explanatory texts on larger screens.

plurals
  1. POBean Validation List<?> on static method or getters
    primarykey
    data
    text
    <p>I have a problem of triggering the custom constraint validation on jersey. I would like to activate the constraint on a method or a static method. What I have tried is putting a custom annotation and <code>@ValidateOnExecution</code> on top of the method, but the custom validator class still was not triggered.</p> <pre><code>@LocationIsValid @ValidateOnExecution public static List&lt;Double&gt; getLocation(String location) { ... } </code></pre> <p>I suspected that the problem is bean annotation did not support static method, so I removed the static keyword and accessing the method by creating a new object. However the custom <code>LocationIsValid</code> validator still was not activated.</p> <p>As a result I ended up placing a validator factory to validate this variable manually. </p> <pre><code>public static List&lt;Double&gt; getLocation(String location) { ... // split the location string into a list of double ... ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); validator = factory.getValidator(); validator.validate(location, LocationIsValid.class); } </code></pre> <p>But yet the custom constraints won't budge. I hope that someone can give me a clue on what to do next, or other suggestion on solving this issue.</p> <p><strong>More Info</strong></p> <p>It works properly when the annotation is placed on top of the resource field. </p> <pre><code>public class Product { ... @LocationIsValid private List&lt;Double&gt; location; ... } </code></pre> <p><strong>Updated</strong></p> <p>Even I have changed the method to normal instance method, it still doesn't work. Note that I have two overloaded methods, one is the resource getter, the other one is for converting the string to locations. </p> <p>Product model</p> <pre><code>public class Product { @Id @JsonSerialize(using = ObjectIdSerializer.class) private ObjectId id; @Size(min = 5) private String name; @NotNull @LocationIsValid private List&lt;Double&gt; location; private Date dateCreated; private Date dateModified; public Product() { } public List&lt;Double&gt; getLocation() { return location; } @ValidateOnExecution @LocationIsValid public List&lt;Double&gt; getLocation(String location) { String[] locationString = location.split(";"); if (locationString.length != 2) { return null; } List&lt;Double&gt;locations = new ArrayList&lt;Double&gt;(); for (int i = 0; i &lt; locationString.length; i++) { locations.add(Double.parseDouble(locationString[i])); } return locations; } // Other setters getters } </code></pre> <p>Product resource</p> <pre><code>@GET public ProductList getProducts(@QueryParam("near") String location) { // parse the locations variable Product product = new Product(); // did not work // I have placed a breakpoint on the LocationIsValid List&lt;Double&gt; locations = product.getLocation(location); } </code></pre> <p>Note: I am pretty sure the problem is not located in LocationIsValid, as it works correctly when I validated it as an entity <code>@Valid Product product</code> I am using Jersey 2.4.1 with jersey-bean-validation 2.4.1 dependency</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload