Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give default date values in requestparam in spring

@RequestMapping(value = "/getSettlements", method = RequestMethod.GET, headers = "Accept=application/json")
  public @ResponseBody
            Collection<Settlement> getSettlements
            (@RequestParam(value = "startDate") String startDate,
            @RequestParam(value = "endDate") String endDate,
            @RequestParam(value = "merchantIds", defaultValue = "null") String merchantIds)

How to give today's date in defaultValue ? It only takes constant.

like image 444
yaSIR Avatar asked Jul 17 '13 07:07

yaSIR


2 Answers

If you are using LocalDate, you can create a default value like this:

@RequestParam(name = "d", defaultValue = "#{T(java.time.LocalDate).now()}", required = true) LocalDate d)
like image 198
yglodt Avatar answered Sep 28 '22 01:09

yglodt


@InitBinder
public void initBinder(WebDataBinder binder) throws Exception {
    final DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    final CustomDateEditor dateEditor = new CustomDateEditor(df, true) {
        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if ("today".equals(text)) {
                setValue(new Date());
            } else {
                super.setAsText(text);
            }
        }
    };
    binder.registerCustomEditor(Date.class, dateEditor);
}

@RequestParam(required = false, defaultValue = "today") Date startDate
like image 31
jaiiye Avatar answered Sep 28 '22 00:09

jaiiye