异常记录之feign.FeignException$NotFound: status 404 reading DeptClientService#getAll()

项目场景:

在微服务中,使用feign出现如下异常异常:

`feign.FeignException$NotFound: status 404 reading DeptClientService#getAll()`

问题描述:

在使用feign实现负载时,访问页面总是出现
在这里插入图片描述
启动类和接口的注解都是用正确,如下图:
在这里插入图片描述
在这里插入图片描述

解决方案:

问题主要出现在接口中的路径和提供者的路径不一致导致的
接口中:

@FeignClient(value = "SPRINGCLOUD-PROVIDER-DEPT")
public interface DeptClientService {


    @GetMapping("/get/{id}")
    public Dept get(@PathVariable("id") Integer id);

    @GetMapping("/dept/get/all")
    public List<Dept> getAll();

}

提供者中:

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @PostMapping("/add")
    public boolean addDept(Dept dept) {
        return deptService.addDept(dept);
    }

    @GetMapping("/selectOne/{id}")
    @HystrixCommand(fallbackMethod = "HystrixGet")
    public Dept get(@PathVariable("id") Integer id) {
        Dept dept = deptService.queryById(id);
        if (dept == null) {
            throw new RuntimeException("id=>"+id+",不存在该用户");
        }
        return dept;
    }

    //备选方案
    public Dept HystrixGet(@PathVariable("id") Integer id) {
        return new Dept()
                .setDeptid(id)
                .setDepname("id=>"+id+",不存在该用户,null----HystrixGet")
                .setDbSource("数据库中没有该信息");

    }

    @GetMapping("/selectAll")
    public List<Dept> getAll(){
        return deptService.queryAll();
    }

}

解决方案:将两者的路径修改一致即可。


版权声明:本文为weixin_45994575原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。