spring cloud-Feign使用中常见问题

1. java.lang.illegalStateException : Method getChildren not annotated with HTTP method type (ex.post,get)

以下两种注解是等价的

@RequestMapping(value = “/system/regions/parent/{parent_id}”, method = RequestMethod.GET)

@GetMapping(“/regions/parent/{parent_id}”)

但是在Feign的使用中,@GetMapping(“”)是不能直接使用的,所以会出现没有指定HTTP类型的异常

2. java.lang.IllegalStateException: PathVariable annotation was empty on param 0

参数标注不能为空,问题类型同上,

public List<Regions> getChildren(@PathVariable int parent_id) {
return regionsManager.listChildren(parent_id);
}

此方法中使用了@PathVariable int parent_id,而在Feign中应该使用@PathVariable(“parent_id”) int parent_id,或@PathVariable(value=”parent_id”) int parent_id

3. Request method ‘POST’ not supported

在参数的传递中,GET方法使用@RequestParam注解,POST方法使用@RequestBody注解,若传递参数时不加注解,则默认为@RequestBody注解,即POST方法,GET方法中有参数不加@RequestParam,被调用的服务就会提示POST是不被支持的

项目调用maven classpath的两种类型

项目根据不同的运行方式,会以不同的方式调用maven classpath:

以eclipse -> run as -> spring boot 运行项目时,若项目中需要调用某个sdk,那么,eclipse会调用此sdk目录下target文件夹中的.class,字节码文件,与maven仓库中的sdk.jar无关。

而以eclipse -> run as -> maven install -> jar -> 命令行java -jar 运行项目时,项目所需依赖会被封装进jar中,而被封装进项目jar包中的sdk,则是取自maven本地仓库。

所以,若出现eclipse运行时成功,而jar包运行失败时,可以考虑重新封装sdk的jar包,替换maven本地仓库中的sdk。