user3580890 user3580890 - 3 years ago 93
Java Question

Can we validate @RequestParam value with a pattern

i have a requirement, where in i need to validate my

such that it is matched with my pattern

Example :

@RequestMapping(value = "/someTest")
public Object sendWishes(@RequestParam("birthDate") String birthDate)

// i need birthDate to be a valid date format in YYYYMMDD format
// if its not valid it should not hit this method

Answer Source

InitBinder will serve purpose. You Should have following init binding code in your controller:

public void initBinder(WebDataBinder binder) {
    SimpleDateFormat dateFormat = new SimpleDateFormat("YYYYMMDD");

    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));

after that you can get birthDate in your specified YYYYMMDD as date object:

@RequestMapping(value = "/someTest")
public Object sendWishes(@RequestParam("birthDate") Date birthDate)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download