org.springframework.beans.factory.BeanCreationException问题。

问题描述 投票:0回答:1

我正在学习春天的引导,我正在创建一个项目,从一个JSON的数据,并把他们在数据库中。

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'userController' method 

这是我的控制器。我将很高兴,如果有人能帮助。谢谢你的帮助!我正在学习spring boot和我的数据库。

@Autowired
private UserService userService;

@GetMapping("/data")
public List<User> getUsers(){

    List<User> usersFinal = userService.getUsers();

    return usersFinal;
}



public String save50User(User user) {
    List<User> usersFinal = userService.getUsers();
    for(int i = 0;i<50;i++) {
        userService.saveUser(usersFinal.get(i));
    }

    return " the first 50 users saved";
}


@GetMapping("/user/{title}")
public List<User> showByTitle(@PathVariable String title) {
    List<User> s = userService.showByTitleLike(title);
    return s;
}


@GetMapping("/user/{id}")
public List<User> showByUserId(@PathVariable Integer id) {
    List<User> s =  userService.showByUserId(id);
    return s;
}


@GetMapping("/user/{id}")
public User showById(@PathVariable int id) {
    User s = userService.showById(id);
    return s;
}
@GetMapping("/user/{completed}")
public List<User> showCompletedTrue(@PathVariable boolean bool) {
    List<User> s = userService.showByCompleted(bool);
    return s;
}
java spring-boot hibernate spring-mvc
1个回答
1
投票

对不起,你所有的GET端点都是含糊不清的,所有的端点都有相同的模式,这使得它们不一样。

手段

@GetMapping("/user/{title}")
@GetMapping("/user/{id}")
@GetMapping("/user/{completed}")

如果你打电话给 用户xyz,它不会识别哪个端点需要被调用,哪个代码需要被执行,因为 xyz可以是标题、id或完成

因此,为了使它与众不同,你应该改变URL模式,如

@GetMapping("/user/title/{title}")
@GetMapping("/user/id/{id}")
@GetMapping("/user/completed/{completed}")

这将使端点与众不同,您的预期业务逻辑将被执行。

© www.soinside.com 2019 - 2024. All rights reserved.