当前位置:网站首页>feignclient @RequestMapping参数设置及请求头简易方式设置
feignclient @RequestMapping参数设置及请求头简易方式设置
2022-07-28 05:27:00 【焱墩】
一、Get请求:
参数为字符串:@RequestParam String param
如果为多个字符串参数:@RequestParam("id") String id, @RequestParam("name") String name
这里可能会想,直接用map不就完了?
举一个例子,如果调用的是第三方的接口,且为:/testServer/test4?id=xxx&name=xxxooo,
这样的方式该如何用feign的方式来调用呢?目前想到的就是用上面的方式即:test4的方式。
如果参数为Map集合或者自定义类,@RequestParam Map map 和字符串没什么区别,所以都用@RequestParam
@Requestparam和@pathvariable 的区别:
(1)、http://localhost:8080/test/login/{id},用@pathvariable
@RequestMapping(value = "login/{id}",method = RequestMethod.GET)
public String login(@PathVariable("id") String id){}
@Pathvariable相当于给参数占位符一个别名,而且只有value值的形式
(2)、http://localhost:8080/test/login?id=xxx&name=xxoo,用@Requestparam
@RequestMapping(value = "login",method = RequestMethod.GET)
public String login(@RequestParam(value = "id",required = false) String id,@RequestParam(value = "name",required = false) String name){}@RequestParam 的属性里面value、和name一样都是设置别名的,required是设置该参数是否必须要串入得属性,默认true。相当于参数里面是key=value的形式。
二、Post请求:
参数为字符串:String param,多个字符串参数都一致
参数为集合或者自定义类:@RequestBody Map map
另外@RequestMapping有params属性可以限制参数,是否为id必须等于1,或者name不能等于2的限制方式
@RequestMapping(value = "login",method = RequestMethod.GET,params = {"id=1","name!=2"})
三、请求头设置:@RequestHeader Map headerMap
这里只用这一种,因为没个调用接口的请求头不一致,所以这种场景可用度高。另外还有统一请求头的方式,主要用在请求头都格式固定的场景。
服务端代码:
@RestController
@RequestMapping("testSever")
public class ServerController {
@RequestMapping(value = "/login", method = RequestMethod.POST)
String login(String param) {
return param;
}
@RequestMapping(value = "/testGet", method = RequestMethod.GET)
String testGet(@RequestHeader Map map) {
return null;
}
@RequestMapping(value = "/test2", method = RequestMethod.GET)
String test2(String param) {
return param;
}
@RequestMapping(value = "/test3", method = RequestMethod.POST)
String test3(String param) {
return null;
}
@RequestMapping(value = "/test4", method = RequestMethod.GET)
byte[] test4() {
return null;
}
@RequestMapping(value = "/test5", method = RequestMethod.GET)
String test5(@RequestParam Map map) {
return null;
}
@RequestMapping(value = "/test6", method = RequestMethod.POST)
String test6(@RequestBody Map map) {
return null;
}客户端feign代码:
@FeignClient(name = "testServer", url = "http://localhost:8899/testServer")
public interface TestRemoteApi {
@RequestMapping(value = "/login", consumes = "application/json;charset=UTF-8", method = RequestMethod.POST)
String login(String param);
@RequestMapping(value = "/testGet", consumes = "application/json;charset=UTF-8", method = RequestMethod.GET)
String testGet(@RequestHeader Map headerMap);
@RequestMapping(value = "/test2", consumes = "application/json;charset=UTF-8", method = RequestMethod.GET)
String test2(@RequestHeader Map headerMap, @RequestParam String param);
@RequestMapping(value = "/test3", consumes = "application/json;charset=UTF-8", method = RequestMethod.POST)
String test3(@RequestHeader Map headerMap, String param);
@RequestMapping(value = "/test4", consumes = "application/json;charset=UTF-8", method = RequestMethod.GET)
byte[] test4(@RequestParam("id") String id, @RequestParam("name") String name);
@RequestMapping(value = "/test5", consumes = "application/json;charset=UTF-8", method = RequestMethod.GET)
String test5(@RequestParam Map map);
@RequestMapping(value = "/test6", consumes = "application/json;charset=UTF-8", method = RequestMethod.POST)
String test6(@RequestBody Map map);
}
后续补充更多方式
边栏推荐
猜你喜欢

【动态规划--买卖股票的最佳时期系列】

Redhawk Dynamic Analysis

2021-11-10

AQS之ReentrantLock源码解析

气传导蓝牙耳机什么牌子好、气传导耳机最好的品牌推荐

What's a good gift for your girlfriend on the Chinese Valentine's day in 2022? Practical and beautiful gift recommendation

图形管线基础(番外篇)

What is hash? (development of Quantitative Trading Robot System)

1、 Ffmpeg record audio as PCM file

SSAO By Computer Shader(二)
随机推荐
七夕礼物送女生什么好?颜值在线又有心意的礼物推荐
用c语言实现三子棋小游戏
What is hash? (development of Quantitative Trading Robot System)
【动态规划--买卖股票的最佳时期系列3】
2022-07-19 达梦数据库-实例创建和管理
Antenna effect solution
OJ 1505 fuse
C语言的动态内存管理函数
刷题记录----反转链表(反转整个链表)
OJ 1284 counting problem
战疫杯--我的账本
气传导耳机哪个品牌比较好、这四款不要错过
2022-05-15 based on JWT token
OJ 1284 记数问题
刷题记录----哈希表
【无标题】
【C语言】字符串库函数介绍及模拟
气传导蓝牙耳机什么牌子好、气传导耳机最好的品牌推荐
AQS之countDownLatch源码分析
雨伞上的水滴效果