gpt4 book ai didi

spring - 如何在 spring 中的 requestparam 中给出默认日期值

转载 作者:行者123 更新时间:2023-12-04 05:31:32 24 4
gpt4 key购买 nike

@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)

如何在 defaultValue 中给出今天的日期?它只需要常数。

最佳答案

@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

关于spring - 如何在 spring 中的 requestparam 中给出默认日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17693435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com