SpringBoot:使用feign调用restful服务时地址栏传参


1.服务提供者(controller层)

@GetMapping("/user/{id}")
public ApiResult getById(@PathVariable("id")Integer id){

    return ok(userService.getById(id));
}

2.Feign模块cilent定义

@FeignClient(value = "api-ucenter-v1",fallbackFactory = UserClientHystrix.class)
public interface UserClient {
    @GetMapping("/user/{id}")
    ApiResult getById(@RequestParam("id") Integer id);
}

重点是这里方法的参数,跟以往拷贝controller中定义的方法不同,这里将 @PathVariable("id")Integer id (从地址栏取参数),改为一般的传参方式@RequestParam("id") Integer id

方法体上的注解不变,还是restful风格

3.通过Feign远程调用

@Autowired
private UserClient userClient;


apiResult = userClient.getById(userModuleDTO.getUserId());

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM