快捷搜索: 王者荣耀 脱发

Feign调用异常FeignException$NotFound: [404] during

1. 问题描述

遇到的问题中很大一部分其实都不是大问题,之所以当时觉得难,要么是基础知识不牢,另一个就是细节。

1.1 问题环境

Springboot项目两个: category项目:端口8080 weimob项目:端口8081 weimob通过FeignClient调用category项目。

  1. category项目
/**
 * (Category)控制层
 *
 * @author makejava
 * @since 2021-06-03 07:20:41
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
          
   
    /**
     * 测试服务
     */
    @GetMapping("/test")
    public Response test() {
          
   
        return Response.createSuccessResponse("查询成功", "我是测试服务");
    }
}
  1. weimob项目
/**
 * 测试
 * @author zrj
 * @since 2021-07-25
 */
@RestController
@RequestMapping("/weimob")
public class ActivityController {
          
   
    @Resource
    private CategoryService categoryService;
    @GetMapping("/test")
    @ApiOperation(value = "微盟获取Code")
    public Response test() throws URISyntaxException {
          
   
        System.out.println("-----测试-----");
        URI uri = new URI("http://localhost:8080/category/test");
        Response response = categoryService.test(uri);
        return Response.createSuccessResponse("查询成功", response);
    }
}

/**
 * 测试接口Openfeign
 * @author zrj
 * @since 2021/7/25
 **/
@Service("WeimobAuthorize")
@FeignClient(url = "http://localhost:8080/category", name = "CategoryService")
public interface CategoryService {
          
   
    @GetMapping("/test")
    Response test(URI uri);
}

1.2 异常信息

feign.FeignException$NotFound: [404] during [GET] to [http://localhost:8080/category/test/test]

2. 问题分析

FeignException$NotFound: [404] during [GET] to [http://localhost:8080/category/test/test] 网上有很多,比如feign接口没有注入,又是没写RestController,或者@Request注解没写等等。

FeignException,肯定是调用异常,首先测试服务项目,调用正常,说明是客户端问题。 [404],肯定是客户端调用问题,没有调用到,首先检查地址端口,FeignClient注册,依赖等等。

通过日志打印发现是路径问题,多了个test,[http://localhost:8080/category/test/test] 。

URI uri = new URI(“http://localhost:8080/category/test”); Response response = categoryService.test(uri); 这里的URI只是替换了service类注解上的url, @FeignClient(url = “http://localhost:8080/category”, name = “CategoryService”) 对于方法上的 @GetMapping("/test")还会在拼接上去,这个很多时候容易忽略,特此记录。

3.解决方案

去掉客户端接口方法上的 @GetMapping("/test"),改为@GetMapping即可。

经验分享 程序员 微信小程序 职场和发展