当前位置:网站首页>Feignclient @requestmapping parameter setting and simple method setting of request header
Feignclient @requestmapping parameter setting and simple method setting of request header
2022-07-28 06:40:00 【Yandun】
One 、Get request :
Parameter is a string :@RequestParam String param
If it is multiple string parameters :@RequestParam("id") String id, @RequestParam("name") String name
Here may be thinking , Direct use map It's over ?
For example , If you call a third-party interface , And for :/testServer/test4?id=xxx&name=xxxooo,
How to use this way feign How to call ? At present, I think of using the above method, that is :test4 The way .
If the parameter is Map Collection or custom class ,@RequestParam Map map It's no different from string , So they all use @RequestParam
@Requestparam and @pathvariable The difference between :
(1)、http://localhost:8080/test/login/{id}, use @pathvariable
@RequestMapping(value = "login/{id}",method = RequestMethod.GET)
public String login(@PathVariable("id") String id){}
@Pathvariable It is equivalent to giving an alias to the parameter placeholder , And only value The form of value
(2)、http://localhost:8080/test/login?id=xxx&name=xxoo, use @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 In the properties of value、 and name The same is set alias ,required Is to set whether the parameter must be concatenated , Default true. Equivalent to... In the parameter key=value In the form of .
Two 、Post request :
Parameter is a string :String param, Multiple string parameters are consistent
Parameters are collections or custom classes :@RequestBody Map map
in addition @RequestMapping Yes params Attributes can restrict parameters , Is it id Must be equal to 1, perhaps name Can not be equal to 2 Restriction mode of
@RequestMapping(value = "login",method = RequestMethod.GET,params = {"id=1","name!=2"})
3、 ... and 、 Request header Settings :@RequestHeader Map headerMap
Only this one is used here , Because the request headers of each calling interface are inconsistent , So this scenario has high availability . There is also a way to unify request headers , It is mainly used in scenarios where the request headers are in fixed format .
Server code :
@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;
}client feign Code :
@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);
}
Follow up more ways
边栏推荐
猜你喜欢

Leetcode 刷题日记 剑指 Offer II 048. 序列化与反序列化二叉树

雨伞上的水滴效果

七夕送女朋友什么礼物好?不会送礼的男生速看!

开放式耳机有哪些、四款音质超好的气传导耳机推荐

刷题记录----二叉树的层序遍历

Several methods of QT setting loading interface

开放式耳机推荐哪款最好、性价比最高的开放式耳机

Explain the installation of MSDN 2015 and precautions

What are the open earphones? Four types of air conduction earphones with excellent sound quality are recommended

Bug experience related to IAP jump of stm32
随机推荐
气传导耳机哪个好、性价比最高的气传导耳机推荐
二维数组实战:螺旋矩阵
2022-07-17 达梦数据库安装
C语言的文件操作
2021-11-10
Listener
【C语言】动态内存管理
ubuntu mysql 设置远程访问权限
关于时间复杂度,你不知道的都在这里
OJ 1284 counting problem
[c语言]--一步一步实现扫雷小游戏
OJ 1018 报数游戏
小程序:wsx脚本
【自我救赎的开始】
OJ 1507 删数问题
2022-05-15 基于jwt令牌token
Leetcode 刷题日记 剑指 Offer II 050. 向下的路径节点之和
2022-06-07 六.日志实现
Hugging face's problem record I
Treasure plan TPC system development DAPP construction