Spring Hibernate验证消息

时间:2016-06-08 22:53:11

标签: java spring internationalization hibernate-validator spring-validator

我有一个Person模型:

public class Person {

    @Min(1)
    private Integer id;

    @Length(min = 5, max = 30)
    @NotEmpty(message = "{NotEmpty.person.name}")
    private String name;

    @Min(value = 0, message = "Min.person.age")
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

我有简单@RestController,我必须验证@RequestBody

@RestController
public class PeopleController {

    private List<Person> people = new ArrayList<>();

    @RequestMapping(method = RequestMethod.POST)
        public List<Person> add(@Valid @RequestBody Person person) {
        people.add(person);
        return people;
    }

}

这是我简单的Spring配置:

@Configuration
public class Configuration {

    @Bean
    public MessageSource messageSource() {
        final ResourceBundleMessageSource source = new ResourceBundleMessageSource();
        source.setBasename("messages");
        return source;
    }

    @Bean
    public Validator validator(MessageSource messageSource) {
        final LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
        validator.setValidationMessageSource(messageSource);
        return validator;
    }
}

messages.properties

Min.person.age=Person's {0} must be greater than {1}
NotEmpty.person.name=Person's {0} cannot be empty
NotNull.person.name=Person's {0} cannot be null
Length.person.name=Person's {0} length should be between {1} and {2}

我还定义了@ControllerAdvice,它只返回绑定中的所有错误消息:

@ControllerAdvice
public class ErrorHandler {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ResponseBody
    public List<String> processValidationError(MethodArgumentNotValidException ex) {
        return ex.getBindingResult()
            .getFieldErrors()
            .stream()
            .map(DefaultMessageSourceResolvable::getDefaultMessage)
            .collect(Collectors.toList());
    }
}    

正如您所看到的,我尝试了不同的变体来访问邮件,但不幸的是,我只收到了默认邮件。

当我发送无效数据时,

{
    "id": 999, // valid
    "name": "121", // invalid - too short
    "age": -123 // invalid - negative
}

回复是:

[
    "length must be between 5 and 30",
    "must be greater than or equal to 0"
]

但应该是:

[
    "Person's age must be greater than 0",
    "Person's name length should be between 5 and 30"
]

我正在使用:

  • org.hibernate:hibernate-validator:5.2.4.Final
  • Spring Boot:1.3.5.RELEASE

我哪里错了?

1 个答案:

答案 0 :(得分:0)

假设&#39; messages.properties&#39;位于类路径的根目录。

@Bean
public MessageSource messageSource() {
    final ResourceBundleMessageSource source = new ResourceBundleMessageSource();
    source.setBasename("classpath:messages");
    return source;
}