Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何自定义java spring@NotNull验证错误消息_Java_Spring_Validation_Spring Boot_Spring Framework Beans - Fatal编程技术网

如何自定义java spring@NotNull验证错误消息

如何自定义java spring@NotNull验证错误消息,java,spring,validation,spring-boot,spring-framework-beans,Java,Spring,Validation,Spring Boot,Spring Framework Beans,大家好,我正在尝试验证通道参数,使其不为空。 如果我将其解析为空,它只会抛出: @POST @Path("") @Consumes({"application/x-www-form-urlencoded"}) TokenDTO login(@ApiParam(value = "The id of the channel used", required = true ) @HeaderParam("channel") @NotEmpty(message = "e

大家好,我正在尝试验证通道参数,使其不为空。 如果我将其解析为空,它只会抛出:

    @POST
    @Path("")
    @Consumes({"application/x-www-form-urlencoded"})
    TokenDTO login(@ApiParam(value = "The id of the channel used", required = true )  @HeaderParam("channel")  @NotEmpty(message = "email.notempty") String channel) throws Exception;
您能告诉我如何自定义验证错误消息吗

编辑:我看到许多文章描述了如何定制“字段”验证的消息。
但是我想验证一个方法参数。

在您的参数中,请确保添加大括号,这样就可以了

{
"code": 26,
"message": "Validation Error: :NotEmpty",
"data": [
    {
        "type": "NotEmpty",
        "property": "",
        "value": null,
        "inputType": "body",
        "attributes": null
    }
],
"source": "XXXX",
"type": "ValidationException",
"guid": "965ee91a-99a1-4ae5-b721-6415a80dad23"
}
然后,在应用程序配置中定义LocalValidatoryFactoryBean:

@NotEmpty(message = "{email.notempty}") String email
最后,在类路径中创建ValidationMessages.properties文件,其中包含:

   @Bean
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:ValidationMessages");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    @Override
    public Validator getValidator() {
        LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
        bean.setValidationMessageSource(messageSource());
        return bean;
    }

我创建了ValidationMessages.properties文件并添加了属性。我仍然收到相同的错误可能您希望添加自定义验证注释,而不是修改现有注释:
email.notempty=E-mail address is required