一、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);
}
后续补充更多方式