0

我有一个Person模型:春天Hibernate的验证消息

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作为:休眠-验证:5.2.4.Final
  • 春季启动:1.3.5.RELEASE

我哪里错了?

+0

看到了这个问题,它可以帮助你http://stackoverflow.com/questions/7093468/how-to-add-custom-error-messages-in-hibernate-validator –

+0

我还具有同样的问题,你有没有解决它,如果是的话,请告诉我如何去做。 – karthi

+0

@karthi嗨。找到解决问题的以下存储库:https://github.com/ITsvetkoFF/Kv-014 –

回答

0

假设'messages.properties'位于classpath的根目录下。

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