Roham Amini
Roham Amini

Reputation: 381

RequestParam.defaultValue value

I am getting:

The value for annotation attribute RequestParam.defaultValue must be a constant expression

but I have declared MAX_LONG_AS_STRING as a constant (static final):

private static final String MAX_LONG_AS_STRING = Long.toString(Long.MAX_VALUE);

@RequestMapping(method = RequestMethod.GET)
public String spittles(@RequestParam(value = "max", defaultValue = MAX_LONG_AS_STRING) long max, 
                       @RequestParam(value = "count", defaultValue = "20") int count, 
                       Model model) {
    model.addAttribute("spittleList", spittleRepository.findSpittles(Long.MAX_VALUE, 20));
    return "spittles";
}

Upvotes: 3

Views: 6931

Answers (3)

Tomas Marik
Tomas Marik

Reputation: 4093

What about:

@RequestParam(defaultValue = Long.MAX_VALUE + "") long max

Upvotes: 4

A0__oN
A0__oN

Reputation: 9100

As chrylis said in his answer annotation take a compile-time constant. This may not be the solution you are looking for but to solve the problem at hand, Long.MAX_VALUE equals 9223372036854775807

So you can do like this:

@RequestMapping(method = RequestMethod.GET)
public String spittles(@RequestParam(value = "max",defaultValue = "9223372036854775807") Long max,
                           @RequestParam(value = "count", defaultValue = "20") int count,
                           Model model){
        model.addAttribute("spittleList",spittleRepository.findSpittle(max,count));
        return "spittles";
}

Upvotes: 3

It's not a compile-time constant; Long.toString() isn't evaluated until runtime. You'll need to inline it (though it's usually better to omit it entirely if possible, and the new QueryDSL support might make building the repository query simpler).

Upvotes: 2

Related Questions