• Ambiguous handler methods mapped for'xxx'报错的解决办法


    这个报错的原因是我们的Controller中,有两个模棱两可的处理方法,这两个方法有歧义,无法分清谁是谁.因为Spring无法根据传参的类型自动匹配到可以处理的方法。比如下面这里,

    @GetMapping("/{id}")和
    @GetMapping("/{addr}")是冲突的,必须修改一下其中的一个url区分开来
    package com.xzit.controller;
    
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    import com.xzit.entity.Emp;
    import com.xzit.service.EmpService;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiModelProperty;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.*;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    @RestController
    @RequestMapping("emp")
    @Api(tags = {"员工管理控制器"})
    public class EmpController {
        @Resource
        private EmpService service;
    
        @ApiOperation("按给定的id显示员工信息")
        @GetMapping("/{id}")
        public Object selectId(@PathVariable Integer id){
            Emp emp=service.selectId(id);
            return emp;
        }
    
        @ApiOperation("按给定地址查询员工信息")
        @GetMapping("/{addr}")
        public Object selectAddr(@PathVariable String addr){
            List<Emp> empList=service.selectAddr(addr);
            return empList;
        }
    }

    执行的报错信息,它分不清这两个方法。 

    正确的修改方式,我在selecAddr()方法随便加一个/userAddr的Url前缀:

    package com.xzit.controller;
    
    import com.github.pagehelper.PageHelper;
    import com.github.pagehelper.PageInfo;
    import com.xzit.entity.Emp;
    import com.xzit.service.EmpService;
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiModelProperty;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.web.bind.annotation.*;
    
    import javax.annotation.Resource;
    import java.util.List;
    
    @RestController
    @RequestMapping("emp")
    @Api(tags = {"员工管理控制器"})
    public class EmpController {
        @Resource
        private EmpService service;
    
    
        @ApiOperation("按给定的id显示员工信息")
        @GetMapping("/{id}")
        public Object selectId(@PathVariable Integer id){
            Emp emp=service.selectId(id);
            return emp;
        }
    
        @ApiOperation("按给定地址查询员工信息")
        @GetMapping("/userAddr/{addr}")
        public Object selectAddr(@PathVariable String addr){
            List<Emp> empList=service.selectAddr(addr);
            return empList;
        }
    }

     重启服务,报错解决,可以正确查询出数据了:

    参考文章:

    https://blog.csdn.net/syc000666/article/details/94978731

  • 相关阅读:
    excel数据 入库mysql 和 mysql数据 导入excel文件
    gson和fastjson将json对象转换成javaBean 简单对照
    docker入门
    jdbc 事务
    关于Java 实现抽象类的抽象方法的特性的利用---面向切面
    try}-with-resources
    关于虚拟机加载类的先后顺序测试
    MySQL api
    JS 截取字符串-全是干货
    JS截取字符串常用方法详细整理
  • 原文地址:https://www.cnblogs.com/zengyu1234/p/16589667.html
Copyright © 2020-2023  润新知