当前位置:网站首页>feign调用get和post记录
feign调用get和post记录
2022-07-28 00:55:00 【RT_0114】
feign调用分get和post最多,其他的put和delete不讲解
1、get请求
调用方传userId和size并绑定@RequestParam,服务提供方会自动解析到userId到User对象的UserId上,解析size到PageRequest 对象的size上。
服务提供方:
参数说明:@PathVariable("organizationId") Long tenantId 服务提供方和服务调用方保持一致User user 调用方无需传实体,传实体内的属性就可以比如userIdPageRequest pageRequest 调用方无需传实体,传实体内的属性就可以比如size
@ApiOperation("查询")
@GetMapping("/v1/{organizationId}/page")
public ResponseEntity<Page<User>> page(@PathVariable("organizationId") Long tenantId, User user, PageRequest pageRequest) {
Page<User> page = PageHelper.doPageAndSort(pageRequest, () -> service.page(user));
return Results.success(page);
}
服务调用方:
参数说明:@PathVariable("organizationId") Long tenantId 服务提供方和服务调用方保持一致@RequestParam("userId") Long userId 是服务提供方第二个参数User的一个属性必须用@RequestParam绑定属性名@RequestParam("size") int size 是服务提供方第三个参数pageRequest的一个属性必须用@RequestParam绑定属性名
feign接口
@FeignClient(
value = "service",
fallbackFactory = RemoteServiceFallbackFactoryImpl.class,
path = "/v1/"
)
public interface RemoteService {
@GetMapping("{organizationId}/page")
ResponseEntity<Page<User>> page(@PathVariable("organizationId") Long tenantId,
@RequestParam("userId") Long userId,
@RequestParam("size") int size);
}
feign异常
@Slf4j
@Component
public class RemoteServiceFallbackFactoryImpl implements RemoteService{
@Override
public ResponseEntity<Page<User>> page(Long tenantId, Long userId, int size) {
log.info("获取数据失败,调用参数:{},{}", userId, size);
throw new CommonException("获取失败");
}
}
controller调用
@Slf4j
@RestController()
@RequestMapping({
"/v1/{organizationId}"})
public class TestController extends BaseController {
@Autowired
PrmRemoteService remoteService;
@GetMapping(value = "/smy-test")
public ResponseEntity<?> smyTest() {
int size = 999;
ResponseEntity<Page<PrLine>> page= remoteService.page(0L, 1L, size);
return Results.success(page);
}
}
2、post请求
服务提供方:
参数说明:@PathVariable("organizationId") Long tenantId 服务提供方和服务调用方保持一致@RequestBody User user 服务提供方和服务调用方保持一致PageRequest pageRequest 调用方无需传实体,传实体内的属性就可以比如size
@PostMapping({
"/v1/{organizationId}/create"})
public ResponseEntity<Page<User>> create(@PathVariable("organizationId") Long tenantId,@RequestBody User user,PageRequest pageRequest) {
Page<User> page = lineService.create(tenantId, user, pageRequest);
return Results.success(page);
}
服务调用方:
feign接口:@PathVariable("organizationId") Long tenantId 服务提供方和服务调用方保持一致@RequestBody User user 服务提供方和服务调用方保持一致PageRequest pageRequest 是服务提供方第三个参数pageRequest的一个属性必须用@RequestParam绑定属性名
@FeignClient(
value = "service",
fallbackFactory = RemoteServiceFallbackFactoryImpl.class,
path = "/v1/"
)
public interface RemoteService {
@PostMapping("/{organizationId}/create")
ResponseEntity<Page<PrLine>> create(@PathVariable("organizationId") Long tenantId,
@RequestBody User user,
@RequestParam("size") int size);
}
feign异常
@Slf4j
@Component
public class RemoteServiceFallbackFactoryImpl implements RemoteService{
@Override
public ResponseEntity<Page<User>> page(Long tenantId, User user, int size) {
log.info("数据失败,调用参数:{},{}", userId, size);
throw new CommonException("行失败");
}
}
controller调用
@Slf4j
@RestController()
@RequestMapping({
"/v1/{organizationId}"})
public class TestController extends BaseController {
@Autowired
PrmRemoteService remoteService;
@GetMapping(value = "/smy-test1")
public ResponseEntity<?> smyTest1() {
int size = 999;
User user= new User();
user.setUserId(888L);
ResponseEntity<Page<PrLine>> page= prmRemoteService.create(21L,user,size);
return Results.success(page);
}
}
总结:
get请求调用方的形参必须都加注解,服务提供方是对象接收,调用方必须用@RequestParam修饰,多个就写多个。
post请求调用方必须有一个@RequestBody注解传对象且只能有一个,若有其他对象接收用@RequestParam修饰,多个就写多个。
边栏推荐
- Promise从入门到精通(第3章 自定义(手写)Promise)
- shell正则和元字符
- Two ways for wechat applet to realize dynamic horizontal step bar
- Unittest单元测试框架全栈知识
- 学会这招再也不怕手误让代码崩掉
- Five basic data structures of redis
- Uniapp summary (applet)
- 智能合约安全——selfdestruct攻击
- MySQL create stored procedure ------ [hy000][1418] this function has none of deterministic, no SQL
- 剑指offer专项突击版第12天
猜你喜欢

Small bulk quantitative stock trading record | data is the source in the quantitative system, which teaches you to build a universal data source framework

Flex布局—固定定位+流式布局—主轴对齐—侧轴对齐—伸缩比
![53: Chapter 5: develop admin management service: 6: develop [admin administrator exit login, interface]; (one point: when we want to modify a value with a certain coding method, the new value should b](/img/9a/674308c7a21fa2be0943ca7967e274.png)
53: Chapter 5: develop admin management service: 6: develop [admin administrator exit login, interface]; (one point: when we want to modify a value with a certain coding method, the new value should b

Under the new retail format, retail e-commerce RPA helps reshape growth

Sample imbalance - entry 0

Redis design specification

Enterprise operation and maintenance practice - using aliyun container image service to pull and build images of overseas GCR and quay warehouses

CeresDAO:Ventures DAO的“新代言”

Interviewer: are you sure redis is a single threaded process?

测试/开发程序员的级别“陷阱“,级别不是衡量单维度的能力......
随机推荐
清除浮动的原因和六种方法(解决浮动飞起影响父元素和全局的问题)
小程序毕设作品之微信校园维修报修小程序毕业设计成品(4)开题报告
[Taichi] draw a regular grid in Tai Chi
学会这招再也不怕手误让代码崩掉
软件测试面试题:常见的 POST 提交数据方式
测试/开发程序员的级别“陷阱“,级别不是衡量单维度的能力......
Software testing interview question: why should we carry out testing in a team?
C # using ABP warehouse to access the database error record set
Go learn 02 basic knowledge
Flume (5 demos easy to get started)
go 学习01
[深入研究4G/5G/6G专题-42]: URLLC-14-《3GPP URLLC相关协议、规范、技术原理深度解读》-8-低延时技术-2-基于slot的调度与Slot内灵活的上下行符号配比
UE4 unreal ndisplay plug-in easy to use three fold screen details
The cooperation between starfish OS and metabell is just the beginning
synchronized详解
探究flex-basis
Talk to ye Yanxiu, an atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
Linux Installation mysql8.0.29 detailed tutorial
mysql创建存储过程---------[HY000][1418] This function has none of DETERMINISTIC, NO SQL
shell正则和元字符