Following example shows how to customize data binding to map query parameters by using @InitBinder and a custom PropertyEditor.
Example
Spring Controller
@Controller
@RequestMapping("/")
public class TradeController {
@InitBinder("tradeDate")
public void customizeBinding(WebDataBinder binder) {
SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd");
binder.registerCustomEditor(Date.class,
new CustomDateEditor(dateFormatter, true));
}
@GetMapping("/trade")
@ResponseBody
public String handleRequest(@RequestParam Date tradeDate) {
return "request received for " + tradeDate;
}
}
Java Config class
@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig {
}
To try examples, run embedded tomcat (configured in pom.xml of example project below):