Feign 传参确保消费者和提供者的参数列表一致 包括返回值 方法签名要一致
在OrderController中写url传参、一个参数传参、2个参数传参、一个对象加一个参数传参
@GetMapping("testUrl/{name}/and/{age}")
public String testUrl(@PathVariable("name")String name,@PathVariable("age") String age){
System.out.println(name+":"+age);
return "ok";
}
@GetMapping("oneParam")
public String oneParam(@RequestParam("name") String name){
System.out.println(name);
return "ok";
}
@GetMapping("twoParam")
public String twoParam(@RequestParam(value = "name",required = false) String name,@RequestParam("age") String age){
System.out.println(name);
System.out.println(age);
return "ok";
}
@PostMapping("oneObjoneParam")
public String oneObjoneParam(@RequestBody Order order,@RequestParam("name") String name){
System.out.println(order);
System.out.println(name);
return "ok";
}
写实体类Order
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class Order {
private Integer id;
private String name;
private Double price;
private Date time;
}
将实体类复制到user-service中,将OrderController中的方法签名放到user-service中的feign接口
@GetMapping("doOrder")
String doOrder();
@GetMapping("testUrl/{name}/and/{age}")
public String testUrl(@PathVariable("name")String name, @PathVariable("age") String age);
@GetMapping("oneParam")
public String oneParam(@RequestParam("name") String name);
@GetMapping("twoParam")
public String twoParam(@RequestParam(value = "name") String name,@RequestParam("age") String age);
@PostMapping("oneObjoneParam")
public String oneObjoneParam(@RequestBody Order order, @RequestParam("name") String name);
在user-service中调用feign从而使用orderController中的方法
@GetMapping("testParam")
public String testParam(){
String sjq = orderFeign.testUrl("四季青","18");
System.out.println(sjq);
String lt = orderFeign.oneParam("11");
System.out.println(lt);
String jg = orderFeign.twoParam("","");
System.out.println(jg);
Order order = Order.builder().name("牛排").price(150D).time(new Date()).id(1).build();
String s = orderFeign.oneObjoneParam(order,"鸡哥");
System.out.println(s);
return "ok";
}
测试:
注意:@RequestParam后面一定要加value具体值@RequestParam(value = “name”) String name,或者@RequestParam(“name”) String name
写一个传递时间的方法
orderController
@GetMapping("testTime")
public String testTime(@RequestParam("time") Date time){
System.out.println(time);
return "ok";
}
user-service的feign接口
@GetMapping("testTime")
public String testTime(@RequestParam("time") Date time);
user-service的Controller进行测试
@GetMapping("time")
public String time(){
Date date = new Date();
System.out.println(date);
String s = orderFeign.testTime(date);
return s;
}
测试:
发现时间不对, ±14个小时,所以不建议单独传递时间参数,建议转换成字符串
@EnableFeignClients(basePackages = {"com.dcits.userserver.feign"})
这段代码将指定包扫描位置,将会加速项目启动
在启动类中写一个bean
/**
* 打印Feign日志信息 级别
* @return
*/
@Bean
public Logger .Level level(){
return Logger.Level.FULL;
}
在yml进行配置
logging:
level:
com.dcits.userserver.feign.UserOrderFeign: debug # 需要打印这个接口下面的日志
启动测试: