0
点赞
收藏
分享

微信扫一扫

open-feign调用接口写法总结

艾米吖 2023-06-18 阅读 83

open-feign调用接口写法总结

POST请求

1、application/json

普通请求

  • 目标接口代码
@PostMapping("/post1")
public String post1(@RequestBody User user) throws JsonProcessingException {
    return new JsonMapper().writeValueAsString(user);
}
  • openFeign请求代码
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
    @Headers({"Content-Type: application/json"})
	@PostMapping("/post1")
	String post1(@RequestBody User user);
}

header中加参数

  • 目标接口代码
//header中增加参数authorization
@PostMapping("/post2")
public String post2(@RequestBody User user, HttpServletRequest request) throws JsonProcessingException {
    String authorization = request.getHeader("authorization");
    return new JsonMapper().writeValueAsString(user)+", authorization=" + authorization;
}
  • openFeign请求代码
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
	@Headers({"Content-Type:application/json","Authorization:{authorization}"})
	@PostMapping("/post2")
    String post2(@RequestHeader(name = "authorization")String authorization,
				 @RequestBody User user);
}

url后面追加参数

  • 目标接口代码
@PostMapping("/post3")
public String post4(@RequestBody User user, HttpServletRequest request,
                    @RequestParam("gg") String gg) throws JsonProcessingException {
    String authorization = request.getHeader("authorization");
    return new JsonMapper().writeValueAsString(user)+", authorization=" + authorization + ", gg="+ gg;
}
  • openFeign请求代码
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
    @Headers({"Content-Type:application/json","Authorization:{authorization}"})
	@PostMapping("/post3")
    String post3(@RequestHeader(name = "authorization") String authorization,
                 @RequestParam("gg") String gg,
				 @RequestBody User user);
}

url中加参数

  • 目标接口代码
@PostMapping("/post4/{userId}")
public String post4(@RequestBody User user, HttpServletRequest request,
                    @PathVariable String userId,
                    @RequestParam("gg") String gg) throws JsonProcessingException {
    String authorization = request.getHeader("authorization");
    return new JsonMapper().writeValueAsString(user)+", authorization=" + authorization + ", userId=" + userId + ", gg="+ gg;
}
  • openFeign请求代码
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
	@Headers({"Content-Type:application/json","Authorization:{authorization}"})
	@PostMapping("/post3/{userId}?gg={gg}")
    String post4(@RequestHeader(name = "authorization")String authorization,
				 @PathVariable("userId") String userId, 
                 @PathVariable("gg") String gg,
				 @RequestBody User user);
}

2、application/x-www-form-urlencoded

  • 目标接口代码
@PostMapping("/post5")
public String post5(User user) throws JsonProcessingException {
    return new JsonMapper().writeValueAsString(user);
}
  • openFeign请求代码
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
    @Headers({"Content-Type: application/x-www-form-urlencoded;charset=UTF-8"})
    @PostMapping("/post5")
    String post5(@RequestBody MultiValueMap<String, Object> user);
}
  • 调用代码
//使用MultiValueMap的实现类LinkedMultiValueMap
//用add方法添加参数
LinkedMultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
multiValueMap.add("name","臭小子");
multiValueMap.add("sex", "男");
feignApiInteferce.post5(multiValueMap);

3、multipart/form-data

  • 目标接口代码
@PostMapping("/post5")
public String post5(User user) throws JsonProcessingException {
    return new JsonMapper().writeValueAsString(user);
}
  • openFeign请求代码
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
    @PostMapping(value = "/post5", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
	String post6(User user);
}

4、参数全拼接到URL中

  • openFeign请求代码
@FeignClient(name = "feignApiClient", url = "http://localhost:${server.port}/produce")
public interface FeignApiInteferce{
	@PostMapping("/post5")
	String post7(@RequestParam("name") String name, @RequestParam("sex") String sex);
}

GET请求

  • 目标接口代码
    @GetMapping("/post6")
    public String post6(User user) throws JsonProcessingException {
        return new JsonMapper().writeValueAsString(user);
    }
  • openFeign请求代码
@FeignClient(name = "feignApiClient", url = "http://localhost:8080/produce")
public interface FeignApiInteferce {
    //一个个传参
	@GetMapping("/post6")
	String post8(@RequestParam("name") String name, @RequestParam("sex") String sex);

    //对象传参用 `@SpringQueryMap` 注解
	@GetMapping(value = "/post6")
	String post9(@SpringQueryMap User user);
}

总结

这里简单总结了一些常见的写法;其它还有像官网的写法通过@RequestLine@param注解的,都差不多;后续遇到了再补充;

demo源码地址

https://gitee.com/shuaidawang/openfeign-demo.git

举报

相关推荐

0 条评论