2016-08-09 52 views
3

Ich erhalte:RequestParam.defaultValue Wert

aber ich habe MAX_LONG_AS_STRING als Konstante (static final) erklärte:

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"; 
} 
+0

Mögliches Duplikat von [Befreie die Meldung "Der Wert für das Annotationsattribut muss ein konstanter Ausdruck sein"] (http://stackoverflow.com/questions/16509065/ge-rid-of-the-value-for-annota-attribute-) muss-ein-konstant-expressio sein n-me) –

+0

Hahaha, das Beispiel stammt eigentlich aus dem Buch "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 = Frühling + in + Aktion)! – badbishop

Antwort

2

Es ist kein Kompilierung- konstant; Long.toString() wird erst zur Laufzeit ausgewertet. Sie müssen es inline einbinden (obwohl es normalerweise besser ist, es wenn möglich komplett wegzulassen, und die neue QueryDSL-Unterstützung könnte das Erstellen der Repository-Abfrage vereinfachen).

3

Als chrylis sagte in seiner answer Annotation eine Kompilierzeitkonstante. Dies kann nicht die Lösung sein, die Sie suchen, aber das Problem bei der Hand zu lösen, Long.MAX_VALUE gleich 9223372036854775807

So können Sie wie folgt tun:

@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"; 
} 
1

Was:

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