创建名为“requestmappinghandlermapping”的bean(在类路径资源中定义)时出错

myzjeezk  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(355)

我很清楚这个问题已经被问过了,但是没有一个答案对我有帮助。stacktrace中提到的“webcontroller”不存在,它以前就存在过。我希望你能帮助我。。

**org.springframework.beans.factory.beancreationexception:创建名为“requestmappinghandlermapping”的bean(在类路径资源中定义)时出错

[org/springframework/boot/autoconfigure/web/servlet/webmvcautoconfiguration$enablewebmvcconfiguration.class]:调用init方法失败;嵌套异常为java.lang.illegalstateexception:不明确Map。无法Map“webcontroller”方法
com.example.demo.controllers.webcontroller#showform(personform)to{get[/register]}:已经有了'registercontroller'bean方法

**

Map了com.example.demo.controllers.registercontroller#showform(personform)。
pom:
4.0.0 org.springframework.boot-spring-boot-starter父级2.4.5 com.example demo 0.0.1-snapshot-demo-spring-boot<java.version>1.8</java.version>org.springframework.boot-spring-boot-starter数据jpa org.springframework.boot-spring-boot-starter验证org.springframework.boot-spring-bootorg.springframework.boot-spring启动程序thymeleaf org.springframework.boot-spring启动程序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>

类别:
@控制器公共类logincontroller实现WebMVCConfiguer{

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

}

}
@控制器公共类注册表控制器实现WebMVCConfiguer{

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

}
@控制器公共类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";
}

}
@控制器公共类用户控制器{

@Autowired
RegisterRepository registerRepository;

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

}
@控制器公共级车辆控制器{

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

}

}

ilmyapht

ilmyapht1#

这个问题就像你有两种相同的方法 Ambiguous mapping Cannot map 在registercontroller中,get和post具有相同的名称,并为这些Map方法指定了一些不同的名称。

hi3rlvi2

hi3rlvi22#

当您有多个具有相同名称和完全相同http predicate (get、post、put、delete)的请求Map时,会发生此异常。
在您的情况下,您必须声明 GET /register 在另一个文件中创建之前的端点Map。
替换这个 GET /register 例如,具有某些前缀的端点 /vehicle/register 使其与之前的Map区分开来。它会解决你的问题。

相关问题