/java/

JSR 303: Bean Validation. Custom message from file

2013-08-24 14:30:39

PostDto.java

...
    @NotNull(message = "{postDto.id.notNull}")
    private Long id;

    @NotEmpty(message = "{postDto.name.notEmpty}")
    private String name;

    @NotNull(message = "{postDto.desc.notNull}")
    @Size(min = 5, max=10, message = "{postDto.desc.size}")
    private String description;
...

src/java/resources/ValidationMessages_en_us.properties

postDto.id.notNull=Id should be pass
postDto.name.notEmpty=Name should be pass
postDto.description.size=Description size must be between {min} and {max}
postDto.description.notNull=Description should be pass

If you want switch language, for example in test you can use:

    @Before
    public void before() {
        Locale.setDefault(new Locale("en_us"));
    }