是否可以在 Java Spring 中使用两个控制器 类 和一个 URI?
Is it possible to use two controller classes with one URI in Java Spring?
我正在做这个项目,我试图访问两个具有相同 URI 的不同控制器。在尝试 运行 之后,我得到了一个 BeanCreationException。
所以碰巧我在创建 bean 时遇到错误。
我希望有办法解决这个问题。
我收到的错误消息:
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
'userController' method public java.lang.String
com.javalanguagezone.interviewtwitter.controller.UserController.overview(java.security.Principal,org.springframework.ui.Model)
to {[/overview],methods=[GET]}: There is already 'tweetController'
bean method
我也在为这个项目使用 Thymleaf。我为这两个控制器设置的 URI:http://localhost:8080/api/overview.The 两个控制器正在为我的 Thymleaf 页面提供我必须与刚才提到的 URI 同时呈现的信息。有了这个,我调用了两个控制器,但我收到了前面提到的错误。
第一个控制器class(TweetController):
@Controller
@Slf4j
public class TweetController {
private TweetService tweetService;
public TweetController(TweetService tweetService) {
this.tweetService = tweetService;
}
@GetMapping( "/overview")
public String tweetsFromUser(Principal principal, Model model) {
model.addAttribute("tweets",tweetService.tweetsFromUser(principal).size());
return "api/index";
}
}
第二个控制器class是:
@Controller
public class UserController {
private UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/followers")
public String followers(Principal principal) {
userService.getUsersFollowers(principal);
return "api/index";
}
@GetMapping("/following")
public int following(Principal principal) {
return userService.getUsersFollowing(principal);
}
@GetMapping("/overview")
public String overview(Principal principal, Model model){
model.addAttribute("followers",userService.getUsersFollowers(principal));
model.addAttribute("following",userService.getUsersFollowing(principal));
return "api/index";
} }
我的问题:有没有办法解决它或者我寻找其他解决方法?我是 Spring 的新手。感谢您的帮助。
根据 REST 约定,您不应该有 /overview,但是 /user/overview。您可以通过在 userController 中提供 @RequestMapping("/user") 来设置它。
以同样的方式你会有“/tweet/overview”端点。
@Controller
@Slf4j
@RequestMapping("/tweet")
public class TweetController {
以任何其他方式进行操作都是违反惯例、违反 Spring 规则的,并且可能意味着您做错了什么。 Spring 不允许两个方法具有相同的 uri,因为它不知道您想要调用哪个方法。
更新:如果需要逻辑,可以发送参数GET: /overview?customParam=user
@GetMapping( "/overview")
public String tweetsFromUser(@RequestParam(value="customParam") String
param, Principal principal, Model model) {
// logic checking customParam...
但是那不能在两个不同的控制器中。指定控制器的唯一方法是通过 base-uri 并且参数不是它的一部分。
Spring 通过 2 个参数确定方法:映射和 HTTP 方法。在这种情况下,除非您手动修改 Spring ,否则没有办法允许第三个参数。另外,没有第三个参数。
或者,您可以使用带映射的第 3 个控制器,当“/overview”端点被触发时调用其他 2 个控制器。在这种情况下,您需要从推文和用户 - 控制器中删除映射。
我正在做这个项目,我试图访问两个具有相同 URI 的不同控制器。在尝试 运行 之后,我得到了一个 BeanCreationException。 所以碰巧我在创建 bean 时遇到错误。 我希望有办法解决这个问题。
我收到的错误消息:
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 'userController' method public java.lang.String com.javalanguagezone.interviewtwitter.controller.UserController.overview(java.security.Principal,org.springframework.ui.Model) to {[/overview],methods=[GET]}: There is already 'tweetController' bean method
我也在为这个项目使用 Thymleaf。我为这两个控制器设置的 URI:http://localhost:8080/api/overview.The 两个控制器正在为我的 Thymleaf 页面提供我必须与刚才提到的 URI 同时呈现的信息。有了这个,我调用了两个控制器,但我收到了前面提到的错误。
第一个控制器class(TweetController):
@Controller
@Slf4j
public class TweetController {
private TweetService tweetService;
public TweetController(TweetService tweetService) {
this.tweetService = tweetService;
}
@GetMapping( "/overview")
public String tweetsFromUser(Principal principal, Model model) {
model.addAttribute("tweets",tweetService.tweetsFromUser(principal).size());
return "api/index";
}
}
第二个控制器class是:
@Controller
public class UserController {
private UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/followers")
public String followers(Principal principal) {
userService.getUsersFollowers(principal);
return "api/index";
}
@GetMapping("/following")
public int following(Principal principal) {
return userService.getUsersFollowing(principal);
}
@GetMapping("/overview")
public String overview(Principal principal, Model model){
model.addAttribute("followers",userService.getUsersFollowers(principal));
model.addAttribute("following",userService.getUsersFollowing(principal));
return "api/index";
} }
我的问题:有没有办法解决它或者我寻找其他解决方法?我是 Spring 的新手。感谢您的帮助。
根据 REST 约定,您不应该有 /overview,但是 /user/overview。您可以通过在 userController 中提供 @RequestMapping("/user") 来设置它。
以同样的方式你会有“/tweet/overview”端点。
@Controller
@Slf4j
@RequestMapping("/tweet")
public class TweetController {
以任何其他方式进行操作都是违反惯例、违反 Spring 规则的,并且可能意味着您做错了什么。 Spring 不允许两个方法具有相同的 uri,因为它不知道您想要调用哪个方法。
更新:如果需要逻辑,可以发送参数GET: /overview?customParam=user
@GetMapping( "/overview")
public String tweetsFromUser(@RequestParam(value="customParam") String
param, Principal principal, Model model) {
// logic checking customParam...
但是那不能在两个不同的控制器中。指定控制器的唯一方法是通过 base-uri 并且参数不是它的一部分。
Spring 通过 2 个参数确定方法:映射和 HTTP 方法。在这种情况下,除非您手动修改 Spring ,否则没有办法允许第三个参数。另外,没有第三个参数。
或者,您可以使用带映射的第 3 个控制器,当“/overview”端点被触发时调用其他 2 个控制器。在这种情况下,您需要从推文和用户 - 控制器中删除映射。