首页 > 其他分享 >Ambiguous handler methods mapped for'xxx'报错的解决办法

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

时间:2022-08-15 21:36:49浏览次数:74  
标签:Ambiguous methods service ApiOperation 报错 import com id addr

这个报错的原因是我们的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

标签:Ambiguous,methods,service,ApiOperation,报错,import,com,id,addr
From: https://www.cnblogs.com/zengyu1234/p/16589667.html

相关文章