【发布时间】:2018-11-11 15:46:59
【问题描述】:
我正在做这个项目,我试图使用相同的 URI 访问两个不同的控制器。在尝试运行它之后,我得到了一个 BeanCreationException。 所以碰巧我在创建一个bean时遇到了一个错误。 我希望有办法解决这个问题。
我得到的错误信息:
org.springframework.beans.factory.BeanCreationException: 错误 创建名称为“requestMappingHandlerMapping”的bean 类路径资源 [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: 调用 init 方法失败;嵌套异常是 java.lang.IllegalStateException:不明确的映射。无法映射 'userController' 方法 public java.lang.String com.javalanguagezone.interviewtwitter.controller.UserController.overview(java.security.Principal,org.springframework.ui.Model) 到 {[/overview],methods=[GET]}: 已经有 'tweetController' 豆方法
我也在为这个项目使用 Thymleaf。我为这两个控制器提供的 URI:http://localhost:8080/api/overview.The 两个控制器正在为我的 Thymleaf 页面提供我必须与刚才提到的 URI 同时呈现的信息。有了这个,我调用了两个控制器,但我得到了前面提到的错误。
第一个控制器类(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";
}
}
第二个控制器类是:
@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的新手。感谢您在高级方面的帮助。
【问题讨论】:
-
您不能从同一个 uri 调用两个相同的映射控制器。这就是您得到不明确映射的原因。您应该更改两个控制器的端点。
标签: java spring controller uri