Error creating bean with name 'requestMappingHandlerMapping' - SpringBoot

陌路散爱 提交于 2021-01-27 19:40:50

问题


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. 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);
    }
}

回答1:


You have created two methods with the same GET request and endpoint, i.e /api/v1/scrcrds/{id}. The solution is to change endpoint for one of your requests either *getSummary()or get(). Here I have changed the endpoint for 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);
    }


来源:https://stackoverflow.com/questions/59662930/error-creating-bean-with-name-requestmappinghandlermapping-springboot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!