1

I know perfectly well that this question has already been asked but none of the answers helped me. The "webController" it mentions in the stacktrace is no present, it existed before. I hope you can help me..

**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 'webController' method

com.example.demo.controllers.WebController#showForm(PersonForm) to {GET [/register]}: There is already 'registerController' bean method

**

com.example.demo.controllers.RegisterController#showForm(PersonForm) mapped.

POM:

4.0.0 org.springframework.boot spring-boot-starter-parent 2.4.5 com.example demo 0.0.1-SNAPSHOT demo Demo project for Spring Boot <java.version>1.8</java.version> org.springframework.boot spring-boot-starter-data-jpa org.springframework.boot spring-boot-starter-validation org.springframework.boot spring-boot-starter-security org.springframework.boot spring-boot-starter-thymeleaf org.springframework.boot spring-boot-starter-web org.thymeleaf.extras thymeleaf-extras-springsecurity5

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

Class:

@Controller public class LoginController implements WebMvcConfigurer {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/a").setViewName("results");
    registry.addViewController("/login").setViewName("login");
    registry.addViewController("/access-denied").setViewName("accessDenied");
}

@GetMapping
public String goToHome(Principal principal){
    if(principal.getName().equalsIgnoreCase("admin"))
    return "Homepage";
    else return "start";

}

}

@Controller public class RegisterController implements WebMvcConfigurer {

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    registry.addViewController("/results").setViewName("results");
}

@Autowired
RegisterRepository registerRepository;

@GetMapping(value = "/register")
public String showForm(PersonForm personForm) {
    return "formValidation";
}

@PostMapping(value = "/register")
public String checkPersonInfo(@Valid PersonForm personForm, BindingResult bindingResult,
                              @RequestParam(value = "nome") String nome,
                              @RequestParam(value = "cognome") String cognome,
                              @RequestParam(value = "dataNascita") String dataNascita,
                              @RequestParam(value = "password") String password) {

    if (bindingResult.hasErrors()) {
        return "formValidation";
    } else if (registerRepository.findByCognome(cognome) == null) {
        Utente utente = new Utente(0, nome, cognome, LocalDate.parse(dataNascita), false, password);
        registerRepository.save(utente);
        UserDetails user =
                User.withDefaultPasswordEncoder()
                        .username(utente.getCognome())
                        .password(utente.getPassword())
                        .roles("USER")
                        .build();

        WebSecurityConfig.inMemoryUserDetailsManager.createUser(user);

        return "results";
    }
    else return "errorRegisterPerson";


}

@PostMapping(value = "/results")
public String returnResults(){
    return "results";
}

}

@Controller public class ShowUserController {

@Autowired
RegisterRepository registerRepository;

@Autowired
UserRepository userRepository;

@GetMapping(value = "/Users")
public String showUsersDB(Model model){
    List<Utente> lista = registerRepository.findAll();
    model.addAttribute("lista", lista);

    return "Users";
}

@GetMapping(value = "/searchUser")
public String searchUserForName(@RequestParam(value = "name")String nome, Model model){
    List<Utente> lista = Collections.singletonList(userRepository.findByCognome(nome));
    model.addAttribute("lista", lista);

    return "Users";
}

@PostMapping(value = "/addUser")
public String addUser(@RequestParam String name,
                      @RequestParam String surname,
                      @RequestParam String date,
                      @RequestParam String password,
                      Model model){

    LocalDate data = LocalDate.parse(date);
    Utente utente = new Utente(0,name,surname,data,false,password);
    registerRepository.save(utente);

    UserDetails user =
            User.withDefaultPasswordEncoder()
                    .username(utente.getCognome())
                    .password(utente.getPassword())
                    .roles("USER")
                    .build();

    WebSecurityConfig.inMemoryUserDetailsManager.createUser(user);

    List<Utente> lista = registerRepository.findAll();
    model.addAttribute("lista", lista);

    return "Users";

}

@GetMapping(value = "/prova/{password}")
public String show(@PathVariable("password")String password, Model model){

    model.addAttribute("oldPassword",password);

    return "UserProfile";
}

@PostMapping(value = "/changeUserPsw/{oldPsw}")
public String changePsw(@PathVariable("oldPsw")String oldPsw,@RequestParam(value = "password")String password,
                        Model model){
    userRepository.setUserPassword(password,oldPsw);

    List<Utente> lista = registerRepository.findAll();
    model.addAttribute("lista", lista);

    return "Users";
}

}

@Controller public class UserController {

@Autowired
RegisterRepository registerRepository;

@GetMapping("/ciaooo")
public String showUserDB(Model model){
    model.addAttribute("lista",registerRepository.findAll());
    return "Users";
}

}

@Controller public class VehiclesController {

@Autowired
VehiclesRepository vehiclesRepository;


@GetMapping(value = "/Vehicles")
public String showVehiclesDB(Model model){
    List<Mezzo> lista = vehiclesRepository.findAll();
    model.addAttribute("lista", lista);

    return "Vehicles";

}

@GetMapping(value = "/searchVehicles")
public String searchVehiclesForName(@RequestParam(value = "name")String nome, Model model){
    List<Mezzo> lista = Collections.singletonList(vehiclesRepository.findByModello(nome));
    model.addAttribute("lista", lista);

    return "Users";
}

@GetMapping(value = "/prova/{targa}")
public String showTarga(@PathVariable("targa")String targa, Model model){

    model.addAttribute("oldTarga",targa);

    return "VehicleProfile";
}

@PostMapping(value = "/changeVehicleTarga/{vecchiaTarga}")
public String changeTarga(@PathVariable("vecchiaTarga")String oldTarga,@RequestParam(value = "targa")String targa,
                        Model model){
    vehiclesRepository.setVehicleTarga(targa,oldTarga);

    List<Mezzo> lista = vehiclesRepository.findAll();
    model.addAttribute("lista", lista);

    return "Vehicles";
}

@PostMapping(value = "/addVehicle")
public String addVehicle(@RequestParam(value = "modello")String modello,
                         @RequestParam(value = "casaCostr")String casaCostr,
                         @RequestParam(value = "tiplogia")String tipologia,
                         @RequestParam(value = "targa")String targa,
                         @RequestParam(value = "annoImm")int annoImm,
                         Model model){

    Mezzo mezzo = new Mezzo(0,targa,modello,casaCostr,tipologia,annoImm);
    vehiclesRepository.save(mezzo);

    List<Mezzo> lista = vehiclesRepository.findAll();
    model.addAttribute("lista", lista);

    return "Vehicles";

}

}

3 Answers 3

0

The issue telling like you have same for two methods that's Ambiguous mapping Cannot map

Within ithe RegisterController you have same for get and post give some different name for those mapping methods.

0

This exception occurs when you have multiple request mapping with the same name and exactly the same HTTP verbs (GET, POST, PUT, DELETE).

In your case, you must have declared a GET /register endpoint mapping before in another file.

Replace this GET /register endpoint with some prefix for example /vehicle/register to make it distinguishable from the previous mapping. It will solve your problem.

1
  • Thank you for response. I resolved this problem doing a build pilots, MAVEN -> Clean Maven and next install. I think that in memory there was another class that i removed but in memory not. Jul 11, 2021 at 10:48
0

This problem has been resolved with: Maven -> clean Maven and next install

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.