2

i made a custom ConstraintValidator , and it work when i assign its annotation to any method , but i noticed that it only make the method to return 500 internal server error and prevent method from print or return any output , but i noticed that any code inside this method still work normally ! , but without return or print ! , so i want to force my custom validator to fully stop execution of the method when the validation return false.

Example of my custom validator

APIAuth

@Documented
@Constraint(validatedBy = APIAuthValidator.class)
@Target({ElementType.METHOD, ElementType.FIELD, ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface APIAuth {
    String message() default "";
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
}

Example of APIAuthValidator.class

public class APIAuthValidator implements ConstraintValidator<APIAuth, String> {

@Context
private HttpServletRequest client;

@Override
public void initialize(APIAuth constraintAnnotation) {
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context) {
    return false;
}

Example of my injected method :

@Stateless
@Path("Accepted")
public class AcceptedAPI {

    @Context
    private UriInfo context;
    @Context
    private HttpServletRequest client;
    @PersistenceContext (unitName="APIPU") EntityManager EM;

    @GET
    @APIAuth
    @Produces(MediaType.TEXT_PLAIN)
    public String getIps() {
        System.out.println("Code Execution Still Work!..");
        return "Test";
    }
}

inside previous code , although of @APIAuth will stop printing of String Test , but others code still work , and method will println "Code Execution Still Work!.." !

Regards,

Jason4Ever
  • 1,439
  • 4
  • 23
  • 43

0 Answers0