1 简介 Feign 传参确保消费者和提供者的参数列表一致 包括返回值 方法签名要一致 1. 通过 URL 传参数, GET 请求,参数列表使用
@PathVariable (“”) 2. 如果是 GET 请求,每个基本参数必须加 @RequestParam (“”) 3. 如果是 POST
请求,而且是对象集合等参数,必须加 @Requestbody 或者 @RequestParam 2 修改 oreder-service-01
2.2 创建 Order 类 package com.it.entity; import lombok.AllArgsConstructor; import
lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; import
java.util.Date; @Data @AllArgsConstructor @NoArgsConstructor @Builder public
class Order { private Integer id; private String name; private Double price;
private Date time; }
2.3 创建 TestParamController 类 package com.it.controller; import
com.it.entity.Order; import org.springframework.web.bind.annotation.*; /** *
url /doOrder/汉堡/add/油条/aaa * get传递一个参数 * get传递多个参数 * post传递一个对象 *
post传递一个对象+一个基本参数 */ @RestController public class ParamController {
@GetMapping("testUrl/{name}/and/{age}") public String
testUrl(@PathVariable("name")String name,@PathVariable("age")Integer age){
System.out.println(name+":"+age); return "ok"; } /** *@RequestParam(required =
false) * 加上括号中的内容,传递参数时,可以不写参数;如果不加,就必须要有参数 */ @GetMapping("oneParam") public
String oneParam(@RequestParam(required = false) String name){
System.out.println(name); return "ok"; } @GetMapping("twoParam") public String
twoParam(@RequestParam(required = false) String name,@RequestParam(required =
false) Integer age){ System.out.println(name); System.out.println(age); return
"ok"; } @PostMapping("oneObj") public String oneObj(@RequestBody Order order){
System.out.println(order); return "ok"; } @PostMapping("oneObjOneParam") public
String oneObjOneParam(@RequestBody Order order,@RequestParam("name") String
name){ System.out.println(order); System.out.println(name); return "ok"; } }
3 修改user-service-02
3.1 将 Order 类 拷贝过来,后面会抽到公共模块里 3.2 修改 UserOrderFeign 接口 package com.it.feign;
import com.it.entity.Order; import
org.springframework.cloud.openfeign.FeignClient; import
org.springframework.web.bind.annotation.*; /** * @FeignClient(value =
"order-service") * value:就是提供者的应用名称 */ @FeignClient(value = "order-service")
public interface UserOrderFeign { @GetMapping("doOrder") public String
doOrder(); @GetMapping("testUrl/{name}/and/{age}") public String
testUrl(@PathVariable("name")String name, @PathVariable("age")Integer age); /**
*@RequestParam(required = false) * 加上括号中的内容,传递参数时,可以不写参数;如果不加,就必须要有参数 */
@GetMapping("oneParam") public String oneParam(@RequestParam(required = false)
String name); @GetMapping("twoParam") public String
twoParam(@RequestParam(required = false) String name,@RequestParam(required =
false) Integer age); @PostMapping("oneObj") public String oneObj(@RequestBody
Order order); @PostMapping("oneObjOneParam") public String
oneObjOneParam(@RequestBody Order order,@RequestParam("name") String name); }
3.3 创建 TestController 类 package com.it.controller; import com.it.entity.Order;
import com.it.feign.UserOrderFeign; import
com.netflix.discovery.converters.Auto; import
org.springframework.beans.factory.annotation.Autowired; import
org.springframework.web.bind.annotation.GetMapping; import
org.springframework.web.bind.annotation.RestController; import java.util.Date;
@RestController public class UserControler { @Autowired public UserOrderFeign
userOrderFeign; /** * feign的默认等待时间是1S *超过1S就会报错 */ @GetMapping("userDoOrder")
public String userDoOrder(){ System.out.println("有用户进来了"); //发起一个远程调用 String s
= userOrderFeign.doOrder(); return s; } @GetMapping("testParam") public String
testParam(){ String xiaoming = userOrderFeign.testUrl("xiaoming", 18);
System.out.println(xiaoming); String s = userOrderFeign.oneParam("小红");
System.out.println(s); String twoParam = userOrderFeign.twoParam("小兰", 22);
System.out.println(twoParam); Order order = Order.builder() .id(01) .name("汉堡")
.price(15D) .time(new Date()) .build(); String oneObj =
userOrderFeign.oneObj(order); System.out.println(oneObj); String oneObjOneParam
= userOrderFeign.oneObjOneParam(order, "小绿");
System.out.println(oneObjOneParam); return "ok"; } }
3.4 测试调用 url传参
get传递一个参数
get传递两个参数
post传递一个对象
post传递一个对象和一个参数
3.5 时间日期参数问题 使用 feign 远程调用时,传递 Date 类型,接收方的时间会相差 14 个小时,是因为时区造成的 处理方案: 1.
使用字符串传递参数,接收方转换成时间类型(推荐使用)不要单独传递时间 2. 使用 JDK8 的 LocalDate( 日期 ) 或 LocalDateTime(
日期和时间,接收方只有秒,没有毫秒) 3. 自定义转换方法 传参总结: get 请求只用来传递基本参数 而且加注解 @RequestParam post
请求用来传递对象参数 并且加注解 @RequestBody