如何使用Spring Rest Controller解决模糊映射问题?(How to resolve ambiguous mapping with Spring Rest Controllers?)

编程入门 行业动态 更新时间:2024-10-27 19:18:57
如何使用Spring Rest Controller解决模糊映射问题?(How to resolve ambiguous mapping with Spring Rest Controllers?)

我查看了以下帖子

1) 创建名为'requestMappingHandlerAdapter'的bean时出错

2) Spring Boot模糊映射。 无法映射方法

3) 找到Spring mvc Ambiguous mapping。 无法映射控制器bean方法

4) Spring MVC模糊映射。 无法映射

但我无法弄清楚如何解决我的问题。 我正在创建一个Spring Boot Web应用程序,我试图将以下端点/quiz/score/{quizId}和/quiz/questions/{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

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

I have looked at the following posts

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

But I have not been able to figure out how to resolve my issue. I am creating a Spring Boot web application in which I am trying to map the following endpoints /quiz/score/{quizId} and /quiz/questions/{quizId} endpoints to two separate methods.

My functions are as follows

@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); }

and

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

I am getting the following error

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

I know that two methods have the same signature, but they have two unique endpoints. How can I resolve this issue?

最满意答案

您的问题是您已经指定了这样的端点:

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

Your problem is that you've specified your endpoints like this:

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

But they should be like this:

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

Note the value instead of name.

For further clarification, you can check RequestMapping javadoc, which explains the different parameters. name parameter just gives a name for your mapping. The value parameter is the key one.

更多推荐

bean,quizId,method,springframework,电脑培训,计算机培训,IT培训"/> <meta name=

本文发布于:2023-08-01 22:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1365711.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   模糊   Rest   Spring   Controllers

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!