当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
Several methods of QT setting loading interface
Leetcode 刷题日记 剑指 Offer II 050. 向下的路径节点之和
当前学习进度
2022-05-24 use of spiel
小程序navigator无法跳转(debug)
OJ 1089 春运
Current learning progress
2022-07-19 Damon database - instance creation and management
量化交易机器人系统开发
gerapy 使用
气传导耳机哪个好、性价比最高的气传导耳机推荐
2022-06-07 六.日志实现
什么气传导蓝牙耳机好、配置比较高的气传导耳机推荐
2022-07-19 Damon database connection instance, execution script, system command
Perl introductory learning (XI) file operation
Listener
小程序:生命周期
OJ 1045 反转然后相加
关于时间复杂度,你不知道的都在这里
Solve the problem that the memory occupation is higher than that of the application process



![[队列,栈的简单应用----包装机]](/img/bc/617b1eb35558c4f948018f593a1de5.jpg)





