How to give default date values in requestparam in spring
Asked Answered
N

4

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

Nightstick answered 17/7, 2013 at 7:12 Comment(0)
R
21
@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
Renteria answered 20/3, 2014 at 4:7 Comment(0)
N
6

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)
Nakamura answered 17/2, 2022 at 20:41 Comment(2)
I like the solution with the SpEL. Can you say what the T is doing here? Some kind of casting?Clericalism
T( type ) creates an instance of the given type.Nakamura
S
2

I tried pretty much every option, even using interceptors. But from far the easiest solution was to use SpEL. For Example: defaultValue = "#{new java.util.Date()}"

Stealing answered 10/9, 2019 at 20:25 Comment(0)
M
0

Since you receive a string you can any date format you want and later on use formatting to extract the date

Melisamelisande answered 17/7, 2013 at 7:15 Comment(8)
I want something like this @RequestParam(value = "merchantIds", defaultValue = date where date has been set up above as String date = new Date().toString(); but its giving error "attribute value can only be constant"Nightstick
You can add a private static String to the class and initialize it when the class loads and use it here.Melisamelisande
thx for info.. I am doing the same .... priavte static String date = new Date().toString(); .... and using this date in my defaultValue but still its saying "attribute value can only be constant"Nightstick
Try adding final in the parameter declarationMelisamelisande
same error ..even after doing private static final String date = new Date().toString();Nightstick
On that case, try the following work-around: set the param to: required = false and in the code inside the method, check for null value and if so - compute it. See the following example: https://mcmap.net/q/439128/-optional-post-parameter-in-spring-mvcMelisamelisande
Yes i was doing the same but was trying to use defaultValue field someway. Just i haven't added required=false . Thanks for help. Collection<SettlementItem> getSettlementItems (@RequestParam(value = "startDate",required = false) String startDate, @RequestParam(value = "endDate" ,required = false ) String endDate, { if(startDate==null) { startDate=date; }Nightstick
Yes - that's exactly it!Melisamelisande

© 2022 - 2024 — McMap. All rights reserved.