12
@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.

4

4 回答 4

19
@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
于 2014-03-20T04:07:34.787 回答
2

我几乎尝试了所有选项,甚至使用拦截器。但到目前为止,最简单的解决方案是使用 SpEL。例如:defaultValue = "#{new java.util.Date()}"

于 2019-09-10T20:25:27.680 回答
1

如果您使用LocalDate,您可以创建一个默认值,如下所示:

@RequestParam(name = "d", defaultValue = "#{T(java.time.LocalDate).now()}", required = true) LocalDate d)
于 2022-02-17T20:41:44.753 回答
0

由于您收到一个字符串,您可以使用任何您想要的日期格式,然后使用格式来提取日期

于 2013-07-17T07:15:56.833 回答