With the bean validation API, you can also validate your method parameters and return values. By validating the method parameters, you can ensure that the business preconditions that should be met have been met, before calling your business code (for example, never withdraw a negative value!), whereas, by validating method return values, you can guarantee the postconditions that should be met after executing your business code (for example, never return a negative value for a calculated outstanding balance!).
To validate a method parameter, just annotate the parameter with the appropriate constraint, as in the following code:
@RequestScoped public class MovieBean { public void createMovie(@NotNull String title) { // do some actions here } } // from another bean movieBean.createMovie (null);
By trying to call the createMovie()
method and passing a null parameter, as shown in the previous example, a validation exception will...