2017-12-03 349 views
1

我已經看過以下職位如何用Spring Rest Control解決模糊映射?

1)Error creating bean with name 'requestMappingHandlerAdapter'

2)Spring Boot Ambiguous mapping. Cannot map method

3)Spring mvc Ambiguous mapping found. Cannot map controller bean method

4)Spring MVC Ambiguous mapping. Cannot map

但我一直無法弄清楚如何解決我的問題。我正在創建一個Spring Boot Web應用程序,其中我試圖將以下端點映射到兩個單獨的方法:/quiz/score/{quizId}/quiz/questions/{quizId}端點。

我的功能如下

@RequestMapping(name="/quiz/questions/{quizId}", method=RequestMethod.GET) 
    public ResponseEntity<QuizQuestion> questions(@PathVariable String quizId) { 
    QuizQuestion question = this.quizService.fetchQuestion(quizId); 
    if (question == null) { 
     return new ResponseEntity<QuizQuestion>(HttpStatus.NOT_FOUND); 
    } 
    return new ResponseEntity<QuizQuestion>(question, HttpStatus.OK); 
    } 

@RequestMapping(name="/quiz/score/{id}", method=RequestMethod.GET) 
    public Score getScore(@PathVariable("id") String quizId) { 
    return this.quizService.getScore(quizId); 
    } 

我收到以下錯誤

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. Cannot map '/myapplication' method 
public com.project.myapplication.Score com.project.myapplication.QuizController.getScore(java.lang.String) 
to {[],methods=[GET]}: There is already '/myapplication' bean method 
public org.springframework.http.ResponseEntity<com.project.myapplication.QuizQuestion> com.project.myapplication.QuizController.questions(java.lang.String) mapped. 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.12.RELEASE.jar:4.3.12.RELEASE] 

. . . . . . . .. . 

Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map '/myapplication' method 
public com.project.myapplication.Score com.project.myapplication.QuizController.getScore(java.lang.String) 
to {[],methods=[GET]}: There is already '/myapplication' bean method 
public org.springframework.http.ResponseEntity<com.project.myapplication.QuizQuestion> com.project.myapplication.QuizController.questions(java.lang.String) mapped. 
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:576) ~[spring-webmvc-4.3.12.RELEASE.jar:4.3.12.RELEASE] 
    at 

我知道這兩種方法具有相同的簽名,但他們有兩個獨特的終點。我該如何解決這個問題?

+0

你能顯示我的應用程序端點嗎? – user7294900

+1

無關注意:您的兩個方法實際上不具有相同的簽名,只是相同的參數。對於相同的簽名,他們需要命名相同,但情況並非如此。方法名稱及其參數類型定義方法簽名。 – eis

回答

3

你的問題是,您所指定的終點是這樣的:

@RequestMapping(name="/quiz/score/{id}", method=RequestMethod.GET) 
public Score getScore(@PathVariable("id") String quizId) { 
    return this.quizService.getScore(quizId); 
} 

但他們應該是這樣的:

@RequestMapping(value="/quiz/score/{id}", method=RequestMethod.GET) 
public Score getScore(@PathVariable("id") String quizId) { 
    return this.quizService.getScore(quizId); 
} 

注意而不是

爲了進一步說明,您可以檢查RequestMapping javadoc,它解釋了不同的參數。 name參數只是爲您的映射提供了一個名稱。 value參數是關鍵之一。