1.零散參數的傳遞
1. querystring的方式 url?id=xxx&name=xxx
被調用的服務方
@GetMapping("/test")
public String test(@RequestParam("id") Integer id, @RequestParam("name") String name){
log.info("接收到的id為 {}, 接受的name為 {}", id, name);
return "products API test";
}
feginclient中
@GetMapping("/test")
public String test(@RequestParam("id") Integer id, @RequestParam("name") String name);
2.RESTFul風格的路徑傳參 url/id/name
被調用的服務方
@GetMapping("/test1/{id}/{name}")
public String test1(@PathVariable("id") Integer id, @PathVariable("name") String name){
log.info("接收到的id為 {}, 接受的name為 {}", id, name);
return "products API test";
}
feginclient中
@GetMapping("/test1/{id}/{name}")
public String test1(@PathVariable("id") Integer id, @PathVariable("name") String name);
2.對象的傳遞
1.使用@RequestBody
被調用的服務方
@PostMapping("/test2")
public String test2(@RequestBody Product product){
log.info("傳參的product信息 {}", product);
return "products API test2";
}
feginclient中
@PostMapping("/test2")
public String test2(@RequestBody Product product);
2.使用@RequestPart
form表單傳遞參數,其中含有文件上傳的信息
3.數組和集合的傳遞
數組
被調用的服務方
@GetMapping("/test3")
public String test3(@RequestParam("ids") String[] ids){
for (String id : ids) {
log.info("id {}", id);
}
return "product test3 API";
}
feginclient中
@GetMapping("/test3")
public String test3(@RequestParam("ids") String[] ids);
集合
使用集合傳遞數據,需要將集合作為一個對象的屬性,再將數據封裝在對象的屬性集合中
@Data
public class ConllectionVO {
private List<String> list;
}
@GetMapping("/test4")
public String test4(ConllectionVO conllectionVO){
conllectionVO.getList().forEach(id -> log.info("id {}", id));
return "product test4 API";
}
@GetMapping("/test4")
public String test4(@RequestParam("list") List<String> list);
list集合其實和數組的數據傳遞並沒有什么太大的區別。只能通過querystring的方式傳遞值 ids=xxx&ids=xxx&ids=xxx
feignclient是一個偽HttpClient,並不能自動的識別參數傳遞,所以需要加注解