简体   繁体   中英

Bean Validation and JAX-WS

Is there a way to get java bean validation 1.1 to work for JAX-WS? I've only seen articles as it applies to JAX-RS. What I'm trying to do is to be able to add validation constraint annotations to specific operations and one to the whole SOAP message or SOAP Body to do a custom validation which uses canonicalization and perhaps Schematron (not XML schema validation.)

Input validation in JAX-WS is normally done on SOAP/XSD Schema/JAXB levels before beans instantiation, that's why you cannot find articles which shows how to use these two things together.

Moreover, bean validation cannot be used for schematron style validation which operates with XML/XPath.

On the other hand, Java bean validation is a specification. It's a part of Java EE which cab be run in Java SE. It can be used with various frameworks and libraries including JAX-WS implementations. The validation process can be triggered manually if bean validation is not natively supported by used framework.

So, generally speaking, it's possible to use bean validation in JAX-WS. It's also possible to implement various custom annotations to support canonicalization. At the same time, in spite of the similarities in the implementation and processing, I would not mix canonicalization with the validation process.

The code below shows a few simple examples how bean validation can be used despite of the context / application framework.

1. Value objects must be annotated

public class Address {

    @Size(max = 100)
    private String City;

    @NotNull
    @Size(max = 200)
    private String Street;

    @Email
    private String email;

    @CountryCode
    private String countryCode;
}

2. Validation process can be triggered manually:

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();

Validator validator = factory.getValidator();

Set<ConstraintViolation<Address>> constraintViolations = validator.validate(address);

3. Custom annotations can be implemented to support specific use cases.

Below is an example of an annotation to be used to validate a country code. Note that the class which performs the validation is included into the annotation code which makes sense in some cases to avoid to many very short classes.

@Documented
@Constraint(
        validatedBy = {CountryCode.CountryCodeValidator.class}
)
@Target({ElementType.METHOD,
        ElementType.FIELD,
        ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface CountryCode {

    String message() default "Unknown Country code";

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

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

    /**
     * Validator implementation
     */
     class CountryCodeValidator implements ConstraintValidator<CountryCode, String> {

        private static final Set<String> ISO_COUNTRIES = 
                 new HashSet<String>(Arrays.asList(Locale.getISOCountries()));

        private CountryCode countryCodeAnnotation;

        @Override
        public void initialize(CountryCode countryCodeAnnotation) {
            this.countryCodeAnnotation = countryCodeAnnotation;
        }

        @Override
        public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
            if (s==null) {
                return true;
            }

            return ISO_COUNTRIES.contains(s);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM