当前位置:网站首页>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);
}
后续补充更多方式
边栏推荐
猜你喜欢
随机推荐
valgrind工具
【动态规划--买卖股票的最佳时期系列】
2022-07-19 达梦数据库 连接实例、执行脚本、系统命令
OJ 1129 fraction matrix
OJ 1505 fuse
2022-07-17 Damon database installation
NFT数藏盲盒+模式系统开发
开放式耳机推荐哪款最好、性价比最高的开放式耳机
图形管线基础(一)
气传导耳机哪个品牌比较好、这四款不要错过
[C note] data type and storage
Redhawk Dynamic Analysis
What are the open earphones? Four types of air conduction earphones with excellent sound quality are recommended
Several methods of QT setting loading interface
【实现简易版扫雷小游戏】
2022-05-15 based on JWT token
Pyppeteer is recognized to bypass detection
Battle plague Cup -- my account book
动态规划--多步爬楼梯(爬楼梯进阶版)
图形管线基础(番外篇)





![[PTA----输出全排列]](/img/66/d1699cd55fa5ff4a55e3e150d02c1b.png)



