“OpenFeign 传递请求体数据”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1My4y1W7vy?p=6”的新页面) |
Jihongchang(讨论 | 贡献) |
||
第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1My4y1W7vy?p=6 | https://www.bilibili.com/video/BV1My4y1W7vy?p=6 | ||
+ | |||
+ | === 被调用的 application service 一方 === | ||
+ | |||
+ | ==== DemoController.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.controller; | ||
+ | |||
+ | import io.github.jihch.pojo.People; | ||
+ | import org.springframework.web.bind.annotation.PathVariable; | ||
+ | import org.springframework.web.bind.annotation.RequestBody; | ||
+ | import org.springframework.web.bind.annotation.RequestMapping; | ||
+ | import org.springframework.web.bind.annotation.RestController; | ||
+ | |||
+ | import java.util.ArrayList; | ||
+ | import java.util.List; | ||
+ | import java.util.Map; | ||
+ | |||
+ | @RestController | ||
+ | public class DemoController { | ||
+ | |||
+ | @RequestMapping("/demo5") | ||
+ | public People demo5() { | ||
+ | return new People(1, "jihch"); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== People.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.pojo; | ||
+ | |||
+ | public class People { | ||
+ | private int id; | ||
+ | private String name; | ||
+ | |||
+ | public int getId() { | ||
+ | return id; | ||
+ | } | ||
+ | |||
+ | public void setId(int id) { | ||
+ | this.id = id; | ||
+ | } | ||
+ | |||
+ | public String getName() { | ||
+ | return name; | ||
+ | } | ||
+ | |||
+ | public void setName(String name) { | ||
+ | this.name = name; | ||
+ | } | ||
+ | |||
+ | public People() { | ||
+ | } | ||
+ | |||
+ | public People(int id, String name) { | ||
+ | this.id = id; | ||
+ | this.name = name; | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | === 应用 OpenFeign 调用的一方 === | ||
+ | |||
+ | ==== FeignDemoController.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.controller; | ||
+ | |||
+ | import io.github.jihch.service.FeignDemoService; | ||
+ | import org.springframework.beans.factory.annotation.Autowired; | ||
+ | import org.springframework.web.bind.annotation.PathVariable; | ||
+ | import org.springframework.web.bind.annotation.RequestMapping; | ||
+ | import org.springframework.web.bind.annotation.RestController; | ||
+ | |||
+ | @RestController | ||
+ | public class FeignDemoController { | ||
+ | |||
+ | @Autowired | ||
+ | private FeignDemoService feignDemoService; | ||
+ | |||
+ | @RequestMapping("/demo4") | ||
+ | public String demo4() { | ||
+ | return feignDemoService.demo4(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== FeignDemoService.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.service; | ||
+ | |||
+ | public interface FeignDemoService { | ||
+ | |||
+ | String demo4(); | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== FeignDemoServiceImpl.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.service.impl; | ||
+ | |||
+ | import io.github.jihch.feign.ServiceDemoFeign; | ||
+ | import io.github.jihch.pojo.Student; | ||
+ | import io.github.jihch.service.FeignDemoService; | ||
+ | import org.springframework.beans.factory.annotation.Autowired; | ||
+ | import org.springframework.stereotype.Service; | ||
+ | |||
+ | @Service | ||
+ | public class FeignDemoServiceImpl implements FeignDemoService { | ||
+ | |||
+ | @Autowired | ||
+ | private ServiceDemoFeign serviceDemoFeign; | ||
+ | |||
+ | @Override | ||
+ | public String demo4() { | ||
+ | Student stu = new Student(); | ||
+ | stu.setId(1L); | ||
+ | stu.setName("张三"); | ||
+ | return serviceDemoFeign.suiyi4(stu).toString(); | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== ServiceDemoFeign.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.feign; | ||
+ | |||
+ | import io.github.jihch.pojo.Student; | ||
+ | import org.springframework.cloud.openfeign.FeignClient; | ||
+ | import org.springframework.web.bind.annotation.PathVariable; | ||
+ | import org.springframework.web.bind.annotation.RequestMapping; | ||
+ | import org.springframework.web.bind.annotation.RequestParam; | ||
+ | |||
+ | @FeignClient("MYPROJECT") | ||
+ | public interface ServiceDemoFeign { | ||
+ | |||
+ | @RequestMapping("/demo5") | ||
+ | Student suiyi4(Student student); | ||
+ | |||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | ==== Student.java ==== | ||
+ | <syntaxhighlight lang="java"> | ||
+ | package io.github.jihch.pojo; | ||
+ | |||
+ | public class Student { | ||
+ | |||
+ | private Long id; | ||
+ | |||
+ | private String name; | ||
+ | |||
+ | public Long getId() { | ||
+ | return id; | ||
+ | } | ||
+ | |||
+ | public void setId(Long id) { | ||
+ | this.id = id; | ||
+ | } | ||
+ | |||
+ | public String getName() { | ||
+ | return name; | ||
+ | } | ||
+ | |||
+ | public void setName(String name) { | ||
+ | this.name = name; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public String toString() { | ||
+ | return "Student{" + | ||
+ | "id=" + id + | ||
+ | ", name='" + name + '\'' + | ||
+ | '}'; | ||
+ | } | ||
+ | } | ||
+ | </syntaxhighlight> |
2023年3月28日 (二) 06:41的版本
https://www.bilibili.com/video/BV1My4y1W7vy?p=6
被调用的 application service 一方
DemoController.java
package io.github.jihch.controller;
import io.github.jihch.pojo.People;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@RestController
public class DemoController {
@RequestMapping("/demo5")
public People demo5() {
return new People(1, "jihch");
}
}
People.java
package io.github.jihch.pojo;
public class People {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public People() {
}
public People(int id, String name) {
this.id = id;
this.name = name;
}
}
应用 OpenFeign 调用的一方
FeignDemoController.java
package io.github.jihch.controller;
import io.github.jihch.service.FeignDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class FeignDemoController {
@Autowired
private FeignDemoService feignDemoService;
@RequestMapping("/demo4")
public String demo4() {
return feignDemoService.demo4();
}
}
FeignDemoService.java
package io.github.jihch.service;
public interface FeignDemoService {
String demo4();
}
FeignDemoServiceImpl.java
package io.github.jihch.service.impl;
import io.github.jihch.feign.ServiceDemoFeign;
import io.github.jihch.pojo.Student;
import io.github.jihch.service.FeignDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class FeignDemoServiceImpl implements FeignDemoService {
@Autowired
private ServiceDemoFeign serviceDemoFeign;
@Override
public String demo4() {
Student stu = new Student();
stu.setId(1L);
stu.setName("张三");
return serviceDemoFeign.suiyi4(stu).toString();
}
}
ServiceDemoFeign.java
package io.github.jihch.feign;
import io.github.jihch.pojo.Student;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("MYPROJECT")
public interface ServiceDemoFeign {
@RequestMapping("/demo5")
Student suiyi4(Student student);
}
Student.java
package io.github.jihch.pojo;
public class Student {
private Long id;
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}