(四)内插约束错误消息

首发博客地址

https://blog.zysicyj.top/

一、默认消息插值

  • 替换默认提示消息

package org.hibernate.validator.referenceguide.chapter04;
public class Car {
    @NotNull(message = "The manufacturer name must not be null")
    private String manufacturer;
    //constructor, getters and setters ...
}

1. 需要转义的字符

  • { 被认为是文字 {

  • } 被认为是文字 }

  • $ 被认为是文字 $

  • \ 被认为是文字 \

2. 消息表达式插值替换

  • 指定消息描述符

package org.hibernate.validator.referenceguide.chapter04.complete;
public class Car {
    @NotNull
    private String manufacturer;
    @Size(
            min = 2,
            max = 14,
            message = "The license plate '${validatedValue}' must be between {min} and {max} characters long"
    )
    private String licensePlate;
    @Min(
            value = 2,
            message = "There must be at least {value} seat${value > 1 ? 's' : ''}"
    )
    private int seatCount;
    @DecimalMax(
            value = "350",
            message = "The top speed ${formatter.format('%1$.2f', validatedValue)} is higher " +
                    "than {value}"
    )
    private double topSpeed;
    @DecimalMax(value = "100000", message = "Price must not be higher than ${value}")
    private BigDecimal price;
    public Car(
            String manufacturer,
            String licensePlate,
            int seatCount,
            double topSpeed,
            BigDecimal price) {
        this.manufacturer = manufacturer;
        this.licensePlate = licensePlate;
        this.seatCount = seatCount;
        this.topSpeed = topSpeed;
        this.price = price;
    }
    //getters and setters ...
}
  • 预期的错误消息

Car car = new Car( null, "A", 1, 400.123456, BigDecimal.valueOf( 200000 ) );
String message = validator.validateProperty( car, "manufacturer" )
        .iterator()
        .next()
        .getMessage();
assertEquals( "must not be null", message );
message = validator.validateProperty( car, "licensePlate" )
        .iterator()
        .next()
        .getMessage();
assertEquals(
        "The license plate 'A' must be between 2 and 14 characters long",
        message
);
message = validator.validateProperty( car, "seatCount" ).iterator().next().getMessage();
assertEquals( "There must be at least 2 seats", message );
message = validator.validateProperty( car, "topSpeed" ).iterator().next().getMessage();
assertEquals( "The top speed 400.12 is higher than 350", message );
message = validator.validateProperty( car, "price" ).iterator().next().getMessage();
assertEquals( "Price must not be higher than $100000", message );

二、自定义消息插值

  • 必须实现javax.validation.MessageInterpolator接口并且实现必须是线程安全的

  • 使用特定的资源包

Validator validator = Validation.byDefaultProvider()
        .configure()
        .messageInterpolator(
                new ResourceBundleMessageInterpolator(
                        new PlatformResourceBundleLocator( "MyMessages" )
                )
        )
        .buildValidatorFactory()
        .getValidator();
  • 使用 AggregateResourceBundleLocator

Validator validator = Validation.byDefaultProvider()
        .configure()
        .messageInterpolator(
                new ResourceBundleMessageInterpolator(
                        new AggregateResourceBundleLocator(
                                Arrays.asList(
                                        "MyMessages",
                                        "MyOtherMessages"
                                )
                        )
                )
        )
        .buildValidatorFactory()
        .getValidator();
```     .getValidator();

最后更新于