Frage

I have a question on JSR cross filed validation. I have a rest based service for get and post. So I have something like

@GET
ItemOfferId getItem(String)

other is that

@Post
boolean setItem(ItemOfferId)

In the ItemOfferId class I have class level annotation called @validoffer. So I have like this class

@validOffer
Class ItemOfferId{
  OfferId offer;
  ItemId item;
}


@Target({ ElementType.TYPE })
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = { OfferValidator.class })
public @interface  ValidOffer {

  String message() default "";
  Class<?>[] groups() default {};

  Class<? extends Payload>[] payload() default {};
}

public class Validator implements ConstraintValidator<ValidOffer,ItemOfferId>{
  //i get here ItemOfferId object and do my custom validation
}

Now my question is that there are some constraints which are specific to " get " service and some are specific to "Set" service. The same object is used in both the methods for get and set. Is there any way to tell the annotation that it is get turn on the get validation. So basically I want to pass some parameters based on run time method call..?

Is it possible..? I have searched a lot in the Internet but could not find the answer... It would be really nice if I could find out how to solve this problem.

Thanks, Swati

War es hilfreich?

Lösung

Have you considered using custom JSR 303 constraint that takes a value. The value is then used by the validator to determine how it should be validated.

So in your case you would use a different value with the validator on post or get validation to tell it which mode to validate in.

There is an example here that shows exactly what you need to do see 3.1 Creating a simple constraint

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top