SpringBoot org.springframework.beans.factory.BeanCreationException:创建名称为'requestMappingHandlerMapping'的bean时出错

时间:2020-02-06 11:23:18

标签: java spring spring-boot

我正在netbeans上的springboot项目中工作。 我收到以下异常:

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 'employeRestService' method 
com.proj_web.controller.EmployeRestService#deleteMany(Long)
to {DELETE /listEmploye/{id}}: There is already 'employeRestService' bean method
com.proj_web.controller.EmployeRestService#delete(Long) mapped.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1796) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550) ~[spring-context-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141) ~[spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215) [spring-boot-2.2.4.RELEASE.jar:2.2.4.RELEASE]
    at com.proj_web.ProjWebApplication.main(ProjWebApplication.java:21) [classes/:na]
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'employeRestService' method 
com.proj_web.controller.EmployeRestService#deleteMany(Long)
to {DELETE /listEmploye/{id}}: There is already 'employeRestService' bean method
com.proj_web.controller.EmployeRestService#delete(Long) mapped.
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.validateMethodMapping(AbstractHandlerMethodMapping.java:632) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:599) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:318) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod(RequestMappingHandlerMapping.java:350) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.registerHandlerMethod(RequestMappingHandlerMapping.java:67) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.lambda$detectHandlerMethods$1(AbstractHandlerMethodMapping.java:288) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at java.util.LinkedHashMap.forEach(LinkedHashMap.java:676) ~[na:1.8.0_73]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:286) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.processCandidateBean(AbstractHandlerMethodMapping.java:258) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:217) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:205) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:171) ~[spring-webmvc-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1855) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1792) ~[spring-beans-5.2.3.RELEASE.jar:5.2.3.RELEASE]
    ... 16 common frames omitted

我尝试再次清理和构建项目,甚至更新了maven,但都没用。

编辑: EmployeRestService


package com.proj_web.controller;

import com.dao.EmployeRepository;
import com.entity.Employe;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

/**
 *
 * @author Nassim Bouhadouf <Nassim.Bouhadouf at https://github.com/nassim-fox/>
 */
@RestController
@CrossOrigin("*")
public class EmployeRestService {

    @Autowired
    EmployeRepository employe_rep ; 

    @GetMapping()
    public List<Object> list() {
        return null;
    }

    @GetMapping("/listEmploye")
    public List <Employe> getListEmploye() {
        return employe_rep.findAll() ;
    }

    @GetMapping("/listEmploye/{id}")
    public Employe getEmploye(@PathVariable(name="id") Long id) {
        return employe_rep.findById(id).get() ;
    }

    @GetMapping("/listEmploye/search/{nom}")
    public List<Employe> getEmployeByNom(@PathVariable(name="nom") String nom) {
        return employe_rep.findByNom(nom) ;
    }

    @PutMapping("/listEmploye/{id}")
    public Employe updateEmp(@PathVariable(name="id") Long id, @RequestBody Employe emp) {
        emp.setId(id) ;
        return employe_rep.save(emp) ; 
    }

    @PostMapping("/listEmploye")
    public Employe save(@RequestBody Employe emp) {
        return employe_rep.save(emp) ;
    }

    @DeleteMapping(value="/listEmploye/{id}")
    public void delete(@PathVariable(name="id") Long id) {
         employe_rep.deleteById(id) ;
    }

    @DeleteMapping(value="/listEmploye/{id}")
    public void deleteMany(@PathVariable(name="id") Long id) {
         employe_rep.deleteById(id) ;
    }

    @PostMapping("/employeCreate/{num}")
    public void createMany(@PathVariable(name="num") Long num,@RequestBody Employe employe) 
    {

            int i  = 0 ; 
            while(i<num)
            {
                employe_rep.save(employe) ; 
                i++ ; 
            }

    }


}

请有人指导我。 谢谢。

5 个答案:

答案 0 :(得分:2)

根据堆栈跟踪,您有重复的方法:

{DELETE /listEmploye/{id}}

由于deletedeleteMany方法的实现是相同的,因此请删除其中之一。

答案 1 :(得分:0)

由于相同的URI,@RequestMapping之后却产生歧义

    @DeleteMapping(value="/listEmploye/{id}")
    public void delete(@PathVariable(name="id") Long id) {
         employe_rep.deleteById(id) ;
    }

    @DeleteMapping(value="/listEmploye/{id}")
    public void deleteMany(@PathVariable(name="id") Long id) {
         employe_rep.deleteById(id) ;
    }

请将其更改为其他内容

示例

        @DeleteMapping(value="/listEmploye/{id}")
        public void delete(@PathVariable(name="id") Long id) {
             employe_rep.deleteById(id) ;
        }

        @DeleteMapping(value="/listEmploye/many/{id}")
        public void deleteMany(@PathVariable(name="id") Long id) {
             employe_rep.deleteById(id) ;
        }

答案 2 :(得分:0)

UpdateEmp,getEmploye,deleteMany具有相同的映射。尝试具有不同的映射。它会工作。对于前。

您的代码:

@DeleteMapping(value="/listEmploye/{id}")
public void delete(@PathVariable(name="id") Long id) {
 employe_rep.deleteById(id) ;
    }
@PutMapping("/listEmploye/{id}")
public Employe updateEmp(@PathVariable(name="id") Long id, @RequestBody Employe emp) {
    emp.setId(id) ;
    return employe_rep.save(emp) ; 
}

我的建议:

@DeleteMapping(value="/listEmployeDelete/{id}")
public void delete(@PathVariable(name="id") Long id) {
 employe_rep.deleteById(id) ;
    }
@PutMapping("/listEmployeUpdate/{id}")
public Employe updateEmp(@PathVariable(name="id") Long id, @RequestBody Employe emp) {
    emp.setId(id) ;
    return employe_rep.save(emp) ; 
}

答案 3 :(得分:0)

该异常表明:您的映射不明确:

@DeleteMapping(value="/listEmploye/{id}")
public void delete(@PathVariable(name="id") Long id) {
     employe_rep.deleteById(id) ;
}

@DeleteMapping(value="/listEmploye/{id}")
public void deleteMany(@PathVariable(name="id") Long id) {
     employe_rep.deleteById(id) ;
}

通过一种方法从@DeleteMapping更改值。

答案 4 :(得分:0)

我认为方法deletedeleteMany都具有相同的映射/listEmploye/{id}。这就是为什么它给歧义映射异常。请以任何一种方法更改映射,它将起作用。

希望它会对您有所帮助。