当前位置:网站首页>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修饰,多个就写多个。
边栏推荐
- Software testing interview question: what types of software testing are you familiar with?
- Appium click operation sorting
- Day6 函数和模块的使用
- Flex development web page instance web side
- Codeforces Round #807 (Div. 2) A-C题解
- 小程序毕设作品之微信校园浴室预约小程序毕业设计成品(1)开发概要
- MySQL高可用和主从同步
- 结构伪类选择器—查找单个—查找多个—nth-of-type和伪元素
- Software testing interview question: why should we carry out testing in a team?
- Go learning 01
猜你喜欢

ArcGIS:加载历史遥感影像

智能合约安全——selfdestruct攻击

C # using ABP warehouse to access the database error record set

Promise from getting started to mastering (Chapter 3: customize (handwriting) promise)

CeresDAO:全球首个基于DAO赋能Web3.0的去中心化数字资产管理协议

Talk to ye Yanxiu, an atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?

IT这个岗位,人才缺口百万,薪资水涨船高,上不封顶

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

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(1)开发概要

LeetCode 热题 HOT 100 -> 3. 无重复字符的最长子串
随机推荐
Vxe table/grid cell grouping and merging
11 Django basics database operation
了解加密行业的“下一个大趋势”——Ventures DAO
UE4 unreal ndisplay plug-in easy to use three fold screen details
数据输出-图片注释、标注
Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
Flume(5个demo轻松入门)
产品解读丨MeterSphere UI测试模块的设计与分布式扩展
Software testing interview question: why should we carry out testing in a team?
LeetCode 热题 HOT 100 -> 2.两数相加
探究flex-basis
Small bulk quantitative stock trading record | data is the source in the quantitative system, which teaches you to build a universal data source framework
Redis design specification
MySQL pymysql operation
软件测试面试题:测试计划工作的目的是什么?测试计划工作的内容都包括什么?其中哪些是最重要的?
The cooperation between starfish OS and metabell is just the beginning
C# 使用Abp仓储访问数据库时报错记录集
样本不均衡-入门0
Data output - dynamic drawing
Flume (5 demos easy to get started)