springboot报错Error creating bean with namerequestMappingHandlerMapping defined in class path resource

关于本人用REST消费者,启动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 'deptConsumerController' method com.jiazhong.springcloud.controller.DeptConsumerController#getAll() to { [/consumer/dept/]}: There is already 'deptConsumerController' bean method com.jiazhong.springcloud.controller.DeptConsumerController#add(Dept) mapped.

  • 路径冲突问题
  • 产生此错误的原因是因为我写消费者Controller时,没注意RESTFUL风的写法…

解决办法

① 修改url
② 修改RequestMapper的method

因为是Restful风格,此处对dept做出处理,不能随意修改不符合Restful风格的url,将@RequestMapper改为对应的@GetMapper@PostMapping即可

  • 改之前
   @RequestMapping("/")
    public Result add(Dept dept){
    
    
        try {
    
    
            restTemplate.postForObject(REST_URL_PREFIX+"/dept/add",dept,Result.class);
            return Result.success("数据添加成功");
        } catch (RestClientException e) {
    
    
            e.printStackTrace();
            return Result.fail(500,"数据添加失败!!!");
        }
    }

    @RequestMapping("/")
    public List<Dept> getAll(){
    
    
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/",List.class);
    }
  • 改之后
    @PostMapping("/")
    public Result add(@RequestBody Dept dept){
    
    
        try {
    
    
            restTemplate.postForObject(REST_URL_PREFIX+"/dept/",dept,Result.class);
            return Result.success("数据添加成功");
        } catch (RestClientException e) {
    
    
            e.printStackTrace();
            return Result.fail(500,"数据添加失败!!!");
        }
    }

    @GetMapping("/")
    public List<Dept> getAll(){
    
    
        return restTemplate.getForObject(REST_URL_PREFIX+"/dept/",List.class);
    }

猜你喜欢

转载自blog.csdn.net/woschengxuyuan/article/details/130301735