![](/img/trans.png)
[英]Error creating bean with name 'processEngine' defined in class path resource
[英]Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource
我非常清楚這個問題已經被問到,但沒有一個答案對我有幫助。 它在堆棧跟蹤中提到的“webController”不存在,它以前存在過。 我希望你能幫幫我..
**org.springframework.beans.factory.BeanCreationException:創建名為“requestMappingHandlerMapping”的bean在類路徑資源中定義時出錯
[org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]:調用init方法失敗; 嵌套異常是 java.lang.IllegalStateException: Ambiguous mapping。 無法映射“webController”方法
com.example.demo.controllers.WebController#showForm(PersonForm) to {GET [/register]}: 已經有 'registerController' bean 方法
**
com.example.demo.controllers.RegisterController#showForm(PersonForm) 映射。
聚甲醛:
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>
班級:
@Controller 公共類 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";
}
}
@Controller 公共類 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";
}
}
@Controller 公共類 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 公共類 UserController {
@Autowired
RegisterRepository registerRepository;
@GetMapping("/ciaooo")
public String showUserDB(Model model){
model.addAttribute("lista",registerRepository.findAll());
return "Users";
}
}
@Controller 公共類 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";
}
}
這個問題告訴你,兩種方法都有相同的Ambiguous mapping Cannot map
在注冊控制器中,你有相同的 get 和 post 為這些映射方法提供一些不同的名稱。
當您有多個具有相同名稱和完全相同的 HTTP 動詞(GET、POST、PUT、DELETE)的請求映射時,會發生此異常。
在您的情況下,您必須先在另一個文件中聲明GET /register
端點映射。
將此GET /register
端點替換為一些前綴,例如/vehicle/register
以使其與之前的映射區分開來。 它會解決你的問題。
此問題已通過以下方式解決:Maven -> clean Maven and next install
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.