2015-05-21 17 views
10

Mam kontroler, który musi zachowywać się inaczej przy różnych parametrach adresu URL. Coś takiego:Metoda kontrolera przeciążenia w Java Spring

@RequestMapping(method = RequestMethod.GET) 
public A getA(@RequestParam int id, @RequestParam String query) { 
    ... 
} 


@RequestMapping(method = RequestMethod.GET) 
public A getA(@RequestParam int id) { 
    ... 
} 

Ale to nie wydaje się działać, pojawia się następujący wyjątek:

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping found. Cannot map '[controller name]' bean method 

Czy istnieje sposób, że aplikacja wybiera sposób w zależności od params URL?

+0

Wyjaśnić _doesn't wydają work_. Czego oczekujesz od tego? Dlaczego masz te oczekiwania? Jak to się właściwie zachowuje? –

+0

Dziękuję za pytania, rozszerzyłem mój post. – wildthing

+0

Po prostu użyj jednej metody i ustaw '' RequestParam' 'required' na 'false'. –

Odpowiedz

27

wskazać w mapowaniu params które powinny być obecne

@RequestMapping(method = RequestMethod.GET, params = {"id", "query"}) 
public A getA(@RequestParam int id, @RequestParam String query) { 
    ... 
} 


@RequestMapping(method = RequestMethod.GET, params = {"id"}) 
public A getA(@RequestParam int id) { 
    ... 
} 
Powiązane problemy