4
    @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;

Hello guys, I am trying to validate the channel parameter so it is not empty. If I parse it empty it just throws me:

{
"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"
}

Can you please tell me how can I customize the validation error message?

EDIT: I have seen many articles describing me how to customize the message of a "Field" validation. But i want to validate a method parameter.

ps138
  • 137
  • 1
  • 11
  • Maybe you want to add a custom validation annotation instead of modifying and existing one: https://stackoverflow.com/questions/46069805/spring-boot-http-get-custom-validation – Blazerg Mar 21 '19 at 10:52

1 Answers1

1

In your parameter make sure to add curly braces, so it will be like this

@NotEmpty(message = "{email.notempty}") String email

Then, define the LocalValidatorFactoryBean in your application configuration:

   @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;
    }

Finally, create a ValidationMessages.properties file in the classpath containing:

email.notempty=E-mail address is required
lkatiforis
  • 5,703
  • 2
  • 16
  • 35
  • i created a ValidationMessages.properties file and added the property. I still get the same error – ps138 Mar 21 '19 at 10:28