2015-03-09 30 views
5

Ciao ho un semplice RestController nel mio campione:SpringBoot @RestController, mappatura ambiguo trovato

@RestController 
public class PersonController { 

    @RequestMapping(name = "/getName", method = GET) 
    public String getName() { 
     return "MyName"; 
    } 

    @RequestMapping(name = "/getNumber", method = GET) 
    public Double getNumber(){ 
     return new Double(0.0); 
    } 
} 

E devo SampleController per l'avvio SpringBoot:

@SpringBootApplication 
@Controller 
public class SampleController { 

    public static void main(String[] args) throws Exception { 
     SpringApplication.run(SampleController.class, args); 
    } 
} 

Quando provo a fare funzionare il seguente SampleCotroller eccezione si verificano:

Caused by: java.lang.IllegalStateException: Ambiguous mapping found. Cannot map 'personController' bean method 
public java.lang.Double com.web.communication.PersonController.getNumber() 
to {[],methods=[GET],params=[],headers=[],consumes=[],produces=[],custom=[]}: There is already 'personController' bean method 
public java.lang.String com.web.communication.PersonController.getName() mapped. 

Dove il problema può essere? Non ci possono essere più RequestMappings in un RestController?

Molto grazie per la risposta

risposta

18

hai di usare l'attributo value per definire la mappatura. In questo momento hai utilizzato name, che fornisce solo un nome alla mappatura, ma non definisce alcuna mappatura. Quindi attualmente entrambi i metodi non sono mappati (nel qual caso entrambi sono mappati sullo stesso percorso). Cambiare i metodi per:

@RequestMapping(value = "/getName", method = GET) 
public String getName() { 
    return "MyName"; 
} 

@RequestMapping(value = "/getNumber", method = GET) 
public Double getNumber(){ 
    return new Double(0.0); 
} 
+0

Grazie mille - ora funziona !!! – Juraj

+0

@JurajKubica Prego. E tu puoi accettare questa risposta allora. –

0

oppure è possibile utilizzare,

@GetMapping("/getName") 

E 'lo stesso uso del metodo con il valore, è una nuova versione di specificare method = "POST" con il valore di richiesta di mappatura.