当前位置:网站首页>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修饰,多个就写多个。
边栏推荐
- [database data recovery] data recovery case of insufficient disk space of SQL Server database
- Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
- synchronized详解
- Redis design specification
- Software testing interview question: what do you think is the key to good test case design?
- C#引入WINAPI传递中文字符串参数字符集问题
- Day6 函数和模块的使用
- 剑指offer专项突击版第12天
- 软件测试面试题:你所熟悉的软件测试类型有哪些?
- Domain Driven Design -- Terminology
猜你喜欢

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

Starfish Os X MetaBell战略合作,元宇宙商业生态更进一步

了解加密行业的“下一个大趋势”——Ventures DAO

Flume(5个demo轻松入门)

Synchronized details

Unity universal red dot system

Fluorite network, difficult to be a "lone brave"

QGIS制图:矢量数据制图流程及导出

Appium 点击操作梳理

53:第五章:开发admin管理服务:6:开发【admin管理员退出登录,接口】;(一个点:我们想要修改一个采用了某种编码方式的值时,新的值最好也按照这种编码方式编码后,再去修改;)
随机推荐
Skywalking distributed system application performance monitoring tool - medium
11-Django-基础篇-数据库操作
微信小程序实现动态横向步骤条的两种方式
CeresDAO:全球首个基于DAO赋能Web3.0的去中心化数字资产管理协议
Codeforces Round #807 (Div. 2) A-C题解
Flex layout learning completed on PC side
了解加密行业的“下一个大趋势”——Ventures DAO
新零售业态下,零售电商RPA助力重塑增长
Causes and solutions of JS digital accuracy loss
54:第五章:开发admin管理服务:7:人脸入库流程;人脸登录流程;浏览器开启视频调试模式(以便能够在本机的不安全域名的情况下,也能去开启摄像头);
11 Django basics database operation
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
Software testing interview question: why should we carry out testing in a team?
Structure pseudo class selector - find single - find multiple - nth of type and pseudo elements
go 学习02 基础知识
Small bulk quantitative stock trading record | data is the source in the quantitative system, which teaches you to build a universal data source framework
uniapp 总结篇 (小程序)
Fluorite network, difficult to be a "lone brave"
Unittest unit test framework full stack knowledge
sftp文件/文件夹上传服务器