Java Spring中的重载控制器方法

编程入门 行业动态 更新时间:2024-10-25 13:24:08
本文介绍了Java Spring中的重载控制器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个控制器需要使用不同的URL参数表现不同。这样的事情:

I have a controller that needs to behave differently with different URL parameters. Something like this:

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

但这似乎不合适为了工作,我得到以下异常:

But this doesn't seem to work, I get the following exception:

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

有没有办法让应用程序选择依赖方法在URL params?

Is there a way that the application chooses the method depending on the URL params?

推荐答案

在映射中指出哪些参数应该存在

Indicate in the mapping which params should be present

@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) { ... }

更多推荐

Java Spring中的重载控制器方法

本文发布于:2023-06-11 23:29:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/645262.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   方法   Java   Spring

发布评论

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

>www.elefans.com

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