gpt4 book ai didi

java - 如何使用 Spring Rest Controller 解决不明确的映射?

转载 作者:行者123 更新时间:2023-11-30 06:46:22 28 4
gpt4 key购买 nike

我看过以下帖子

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

我知道两个方法具有相同的签名,但它们有两个唯一的端点。我该如何解决这个问题?

最佳答案

你的问题是你指定了这样的端点:

@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 参数是关键参数。

关于java - 如何使用 Spring Rest Controller 解决不明确的映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47616590/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com