Use jsr303 for parameter verification

Source: Internet
Author: User

JSR 303-bean validation is a data validation specification.

At any time, when you want to process the business logic of an application, you must consider data verification to ensure that the input data is correct in terms of semantics. In general, applications are layered, and different layers are done by Different developers. In many cases, the same data verification logic may appear on different layers, which may cause code redundancy and is not conducive to maintenance. Bean validation can be used to bind the verification logic to the corresponding domain model, which can effectively avoid such problems.

Bean validation defines the corresponding metadata model and API for JavaBean verification. The default metadata is Java annotations. In an application, you can use bean validation or your own constraint, for example@NotNull,@Max,@SizeTo ensure the correctness of the data model (JavaBean. Bean validation is a runtime data verification framework. After verification, the verification error message is immediately returned.

Hibernate validator is a reference implementation of bean validation. hibernate validator provides all built-in constraint implementations in the JSR 303 specification, in addition to some additional constraint. Hibernate validator can be used for parameter verification.

The example is as follows (from reference 2 ):

1. MAVEN dependency:

<dependency><groupId>com.alibaba.external</groupId><artifactId>sourceforge.hibernate.validator</artifactId><version>4.0.2.GA</version></dependency><dependency><groupId>com.alibaba.external</groupId><artifactId>java.validation.api</artifactId><version>1.0.0.GA</version></dependency><dependency><groupId>com.alibaba.external</groupId><artifactId>org.slf4j.slf4j-api</artifactId><version>1.5.6</version></dependency><dependency><groupId>com.alibaba.external</groupId><artifactId>org.slf4j.slf4j-log4j12</artifactId><version>1.5.6</version></dependency><dependency><groupId>com.alibaba.external</groupId><artifactId>jakarta.log4j</artifactId><version>1.2.16</version></dependency><dependency><groupId>com.alibaba.external</groupId><artifactId>sourceforge.spring</artifactId><version>2.5.6</version></dependency>

2. Java Bean to be verified

package com.mycompany;import javax.validation.constraints.Min;import javax.validation.constraints.NotNull;import javax.validation.constraints.Size;public class Car {    @NotNull    private String manufacturer;    @NotNull    @Size(min = 2, max = 14)    private String licensePlate;    @Min(2)    private int seatCount;    public Car(String manufacturer, String licencePlate, int seatCount) {        this.manufacturer = manufacturer;        this.licensePlate = licencePlate;        this.seatCount = seatCount;    }    //getters and setters ...}

3. Verification

import javax.validation.Validation;import javax.validation.Validator;import javax.validation.ValidatorFactory;import org.junit.BeforeClass;import org.junit.Test;public class CarTest {private static Validator validator;    @BeforeClass    public static void setUp() {        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();        validator = factory.getValidator();    }    @Test    public void manufacturerIsNull() {        Car car = new Car(null, "DD-AB-123", 4);        Set<ConstraintViolation<Car>> constraintViolations =        validator.validate(car);        assertEquals(1, constraintViolations.size());        assertEquals("may not be null",             constraintViolations.iterator().next().getMessage());    }    @Test    public void licensePlateTooShort() {        Car car = new Car("Morris", "D", 4);        Set<ConstraintViolation<Car>> constraintViolations =             validator.validate(car);        assertEquals(1, constraintViolations.size());        assertEquals("size must be between 2 and            14", constraintViolations.iterator().next().getMessage());    }}

For more information, see document 2.

Reference:

1. JSR 303: http://jcp.org/en/jsr/summary? Id = 303

2, Hibernate validator: http://docs.jboss.org/hibernate/validator/4.2/reference/en-US/pdf/hibernate_validator_reference.pdf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.