简体   繁体   中英

org.springframework.beans.factory.BeanCreationException problem

I'am learning spring boot and I am creating a project that take data from a JSON and put them in a database.While trying to create the GET methods I had this exception:

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 

here is my controller. I will be happy if someone can help.. Thank you !

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

Sorry but all your Get Endpoints are ambiguous all having the same pattern which does not make them distinct

Means

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

If you call the /user/xyz , It won't recognise which endpoint needs to be called and which code needs to be executed because xyz can title, id or completed

So to make it distinct you should change the url pattern like

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

this will make the endpoints distinct and your expected business logic would be executed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM