0

I am new in validation.

controller:

@Validated
@RestController
public class AccountController
{
    @PostMapping( value = "/account/location" )
    public ResponseEntity<LocationAccountVO> createLocationAccount( @RequestHeader 
HttpHeaders headers,@Valid @RequestBody LocationAccountVO locationAccountVO ) throws 
NumberParseException
    {
        return ResponseEntity.status(HttpStatus.CREATED).body( 
accountService.createLocationAccount( locationAccountVO ) );
    }
}

LoacationVo.java:

@Data
@JsonInclude( JsonInclude.Include.NON_NULL )
public class LocationAccountVO
{
private UUID locationId;
    @NotNull(message = "Email is mandatory.")
    @Email( regexp = ValidationConstant.EMAIL, message = "Email be valid")
    private String email;
}

public static final String EMAIL = "\\\\b[A-Z]+@[A-Z0-9.-]+\\\\.[A-Z]{2,4}\\\\b";

but @Email not giving custom message and pattern also not working.Kindly solve my problem.

Thanks.

11
  • Replace 4 backslash sequences with \\. Actually, the \\b at both ends are redundant, remove them. Commented Aug 24, 2018 at 10:04
  • @WiktorStribiżew \b[A-Z]+@[A-Z0-9]+\\.[A-Z]{2,4}\b is this correct? Commented Aug 24, 2018 at 10:10
  • I don't know Hibernate validation but unless it is case-insensitive all emails would have to use upper case only and your regex also would not allow for emails like [email protected] Commented Aug 24, 2018 at 10:10
  • @Thomas is this correct ^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@? Commented Aug 24, 2018 at 10:11
  • 1
    You do not need anchors if the pattern is used with matches, public static final String EMAIL = "[A-Z]+@[A-Z0-9.-]+\\.[A-Z]{2,4}"; should be enough. But it is too restrictive. Consider something like "\\S+@\\S+\\.\\S+" Commented Aug 24, 2018 at 10:14

2 Answers 2

2

You can implement like that.

@Data
@JsonInclude( JsonInclude.Include.NON_NULL )
public class LocationAccountVO
{
private UUID locationId;
    @NotEmpty(message = "Email is mandatory.")
    @Pattern(regexp = "^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", message = "Email be valid")
    private String email;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have used that and it's working check the following link github.com/faizakram/Application
0

1- Add the necessary dependencies in pom.xml file from Maven Central:

  • spring-boot-starter-web
  • spring-boot-starter-validation

2- Add the necessary Annotations in your form bean:

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

3- To read messages from message.properties, you have to define the MessageSource Bean

@Bean
public ResourceBundleMessageSource messageSource() {
    final ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
    messageSource.setBasename("messages");
    messageSource.setUseCodeAsDefaultMessage(Boolean.parseBoolean("true");
    messageSource.setDefaultEncoding("UTF-8");
    return messageSource;
}

4- To avoid having the custom validator ignored, you have to set the validator by overriding the getValidator() method. Set validation message source to message.properties.

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

5- in the message.properties file under src/main/resources just define a property message like:

email.notempty=This field is required.

note : If it does not work yet you can still do this.

javax.validation.constraints.Email.message=Please provide valid email id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.