feignclient @RequestMapping参数设置及请求头简易方式设置

一、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);
}

后续补充更多方式

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

焱墩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值