通过@FeignClient注解调用微服务接口,若接口路径参数不传入,会导致接口调用失败

 

举个例子,如下接口 如图1-1:

 

/vl/{organizationld}/runtime/prc/end/{procld} Implementation Notes "menu perm issi on Level": "organiza tion", "roles": 0, "permission Ise," perm issi on Public ":false, "permissio nWithin ":false, "status eApiPath n: null, "upgra "labe l": Parameters Parameter Value (required) Description procld Response Model Parameter Type path o Data Type string Headers Response Messages HTTP Status code Reason OK Unauthorized Forbidden Not Found Try it out'

图1-1 接口详情

查看该接口的源码,如图1-2:

@Api ( tags — @Restcontr011er @RequestNapping ( ( /vl/ } ) public el ass extends Base-controller {​

@permission ( level = ResourceLeve1 . ORGANIZATION @Apioperation ( '4 " ) @GetMapping ( ( " /runtime/prcZ/ {procld} " ) @Responsestatus (Ht tpstatus . 0K) public ResponseEntity<Void> endproc (Httpserv1etRequest request, this. activitiservice . deleteprocesslnstance (procld) ; return Results . success ; @pathvariable String procld)

图1-2接口源码

接口路径当中有{organizationId} 接口路径参数,但是接口入参的时候中并没有接收该参数,

一般人会以为,既然不接收该参数,那就不传进去就行,可是,这种想法是错误的。

 

错误写法:

通过@FeignClient调用该接口,这种错误写法会导致接口调用失败,如图1-3

 

@EeignCIient ( value = Workflow. NAME, fallback = WorkEIowEeignCIientEallback. public interface ( @param proc rd d turn @GetMapping (value = " / VI/ {organizationld} 'runtime/prc/end/ {procld) " ) endproc (HttpSerVIetRequest request, @pathvariable (name = "procld") String procld) ;

图1-3错误写法

 

正确的写法:

应该是把organizationId作为接口路径参数传入,如图1-4:

 

 

*ignC1ient (value HZeroService .Workf10w. NNE, public Interface WorkF10wFeignC1ient * ppaz•am procld * exec urn fallback — WorkF10wFeignC1ientFa11back. class) / ( organizationld} / runtime 'pre/ end/ {procld) ) @GetMapping (value — ResponseEntity<Void> endProc (@PathVariab1e (name — "organizationld") String organizationld, @PathVariab1e (name — "procld") String procld) ;

图1-4正确写法

总结:

传不传参合和它接不接这个参数没有任何关系    路径里包含这个参数   但是接口没有接这个参数

按理来说可以不传,但是还有其他的影响,网关会截取这个参数做鉴权 所以还是要传

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐