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

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

我非常清楚这个问题已经被问过,但没有一个答案对我有帮助。它在堆栈跟踪中提到的“webController”不存在,它之前就存在。我希望你能帮助我..

**org.springframework.beans.factory.BeanCreationException:创建类路径资源中定义的名为“requestMappingHandlerMapping”的bean时出错

[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]:调用init方法失败;嵌套异常是 java.lang.IllegalStateException:不明确的映射。无法映射“webController”方法

com.example.demo.controllers.WebController#showForm(PersonForm) 到 {GET [/register]}:已经有 'registerController' bean 方法

**

com.example.demo.controllers.RegisterController#showForm(PersonForm) 已映射。

POM:

4.0.0 org.springframework.boot spring-boot-starter-父级 2.4.5 com.example 演示 0.0.1-快照 演示 Spring Boot 的演示项目 1.8 org.springframework.boot spring-boot-starter-数据-jpa org.springframework.boot spring-boot-starter-验证 org.springframework.boot spring-boot-starter-安全性 org.springframework.boot spring boot 启动程序 thymeleaf org.springframework.boot spring-boot-启动器-web org.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 实现 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";

}

}

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

}

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

}

@控制器 公共类 UserController {

@Autowired
RegisterRepository registerRepository;

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

}

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

}

}

java spring spring-boot maven
4个回答
1
投票

当您有多个具有相同名称和完全相同的 HTTP 动词(GET、POST、PUT、DELETE)的请求映射时,会发生此异常。

在您的情况下,您必须之前在另一个文件中声明了

GET /register
端点映射。

将此

GET /register
端点替换为一些前缀,例如
/vehicle/register
,以使其与之前的映射区分开来。它会解决你的问题。


0
投票

问题就像你有两种方法一样,那就是

Ambiguous mapping Cannot map 

在 RegisterController 中,您有相同的 get 和 post 为这些映射方法提供一些不同的名称。


0
投票

当我改变路径然后它起作用时,我也面临同样的问题, Spring 不允许相同的 URL 映射。 enter image description here


-1
投票

此问题已通过以下方式解决:Maven -> clean Maven and next install

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