3

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";
}
Roham Amini
  • 361
  • 3
  • 12
  • Possible duplicate of [Get rid of "The value for annotation attribute must be a constant expression" message](http://stackoverflow.com/questions/16509065/get-rid-of-the-value-for-annotation-attribute-must-be-a-constant-expression-me) – Vipin CP Aug 09 '16 at 07:06
  • Hahaha, the example is actually from the book "Spring in Action" (https://www.amazon.com/Spring-Action-Covers-4/dp/161729120X/ref=sr_1_1?s=books&ie=UTF8&qid=1490873783&sr=1-1&keywords=spring+in+action)! – badbishop Mar 30 '17 at 11:39

3 Answers3

4

What about:

@RequestParam(defaultValue = Long.MAX_VALUE + "") long max
Tomas Marik
  • 4,053
  • 3
  • 31
  • 62
3

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";
}
Community
  • 1
  • 1
A0__oN
  • 8,740
  • 6
  • 40
  • 61
2

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

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152