简体   繁体   English

创建名为“requestMappingHandlerMapping”的 bean 时出错 - SpringBoot

[英]Error creating bean with name 'requestMappingHandlerMapping' - SpringBoot

I am getting the following error at runtime.我在运行时收到以下错误。

Exception encountered during context initialization - cancelling refresh attempt: 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 'scrcrdsController' method 
com.mastercard.qualityScore.controllers.ScrcrdsController#getSummary(String)
to {GET /api/v1/scrcrds/{id}}: There is already 'scrcrdsController' bean method
com.mastercard.qualityScore.controllers.ScrcrdsController#get(String) mapped.

I need to ccreate a new api which fetches all the scores of a particular id or maybe a combination of two id's.我需要创建一个新的 api,它获取特定 id 的所有分数,或者可能是两个 id 的组合。 How do I fix this?我该如何解决? Please help.请帮忙。

My controller is as follows-我的控制器如下-

@RestController
@RequestMapping("/api/v1/scrcrds")
public class ScrcrdsController {
    @Autowired
    private ScrcrdRepository scrcrdRepository;

    //list all scrcrd records
    @GetMapping
    public List<Scrcrd> list() {
        return scrcrdRepository.findAll();
    }

    //get summary  of an employee
    @GetMapping(value = "{id}")
    public List<Scrcrd> getSummary(@PathVariable String id) {
        return  scrcrdRepository.findAllById(Collections.singleton(id));
    }

    //get scrcrd record by id
    @GetMapping(value = "{id}")
    public Scrcrd get(@PathVariable String id) {
        return scrcrdRepository.getOne(id);
    }

    //create a new scrcrd record
    @PostMapping
    @ResponseStatus(HttpStatus.CREATED)  //to get  201 response instead of 200
    public Scrcrd create(@RequestBody final Scrcrd scrcrd) {
        return scrcrdRepository.saveAndFlush(scrcrd);
    }

    //delete a scrcrd record
    @RequestMapping(value = "{id}", method = RequestMethod.DELETE)
    public void delete(@PathVariable String id) {
        //Also need to check for children records before deleting
        scrcrdRepository.deleteById(id);
    }

    //update a scrcrd record
    @RequestMapping(value = "{id}", method = RequestMethod.PUT)
    public Scrcrd update (@PathVariable String id, @RequestBody Scrcrd scrcrd) {
        //because this is a PUT, we expect all attributes to be passed in. A PATCH would only need what attribute is being modified
        //TODO: Add validation that all attributes are passed in, otherwise return a 400 bad payload
        Scrcrd existingScrcrd = scrcrdRepository.getOne(id);
        //attributes of emp are copied to existingScrcrd, emp_id, mgr_id, mgr_scr_dt and type_nam is not to be changed
        BeanUtils.copyProperties(scrcrd, existingScrcrd   , "emp__id", "mgr_id", "mgr_scr_dt", "type_nam");
        return scrcrdRepository.saveAndFlush(existingScrcrd);
    }
}

You have created two methods with the same GET request and endpoint, ie /api/v1/scrcrds/{id} .您已经创建了两个具有相同 GET 请求和端点的方法,即/api/v1/scrcrds/{id} The solution is to change endpoint for one of your requests either *getSummary()or get() .解决方案是更改 *getSummary() 或get()请求之一的端点。 Here I have changed the endpoint for getSummary() .在这里,我更改了getSummary()的端点。

    //get summary  of an employee
    @GetMapping(value = "summary/{id}")
    public List<Scrcrd> getSummary(@PathVariable String id) {
        return  scrcrdRepository.findAllById(Collections.singleton(id));
    }

    //get scrcrd record by id
    @GetMapping(value = "{id}")
    public Scrcrd get(@PathVariable String id) {
        return scrcrdRepository.getOne(id);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复错误创建名为 &#39;requestMappingHandlerMapping&#39; 的 bean - How to fix Error creating bean with name 'requestMappingHandlerMapping' SpringBoot org.springframework.beans.factory.BeanCreationException:创建名为“requestMappingHandlerMapping”的 bean 时出错 - SpringBoot org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' 使用在类路径资源中定义的名称“requestMappingHandlerMapping”创建 bean 时出错 - Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource 创建名称为&#39;org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping的bean时出错 - Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping Tomcat上的SpringBoot:创建名称为“ jacksonObjectMapperBuilder”的bean时出错 - SpringBoot on Tomcat: Error creating bean with name 'jacksonObjectMapperBuilder' SpringBoot MAVEN 和 MongoDB - 使用名称创建 bean 时出错 - SpringBoot MAVEN and MongoDB -Error creating bean with name SpringBoot 测试 - 创建名为“entityManagerFactory”的 bean 时出错 - SpringBoot Test - Error creating bean with name 'entityManagerFactory' 初始化 springboot 创建名为“flywayInitializer”的 bean 时出错 - Error on initialize springboot creating bean with name 'flywayInitializer' 创建名为“org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping”的bean时出错 - Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping' 创建名为 &#39;org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0 的 bean 时出错 - Error creating bean with name 'org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping#0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM