- 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;
- }
- 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";
- }
-
-
- }
- 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);
-
- }
- 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";
- }
-
-
- }