feign传递数组_解决:SpringCloud中Feign支持GET请求POJO传参

本文介绍如何解决SpringCloud Feign在GET请求中不支持POJO参数的问题。通过添加Maven依赖,启用Hystrix和HTTPClient,以及实现一个拦截器`FeignRequestInterceptor`,将POJO转换为查询参数,使得Feign可以处理包含多个参数的GET请求。
摘要由CSDN通过智能技术生成

在日常的开发中,当遇到一个请求需要传递多个参数时,我们习惯将参数封装到一个POJO对象中,已提高程序的可读性和简化编写。但是在使用SpringCloud时,Feign对SpringMVC注解支持并不完善,其中一点就是,当发送的GET请求携带多个参数时,不能使用POJO来封装参数,这个就比较蛋疼了。一种使之支持GET请求POJO传递参数的方法如下:

添加Maven依赖

org.springframework.cloud

spring-cloud-starter-openfeign

com.netflix.feign

feign-httpclient

8.15.1

io.github.openfeign

feign-hystrix

10.2.0

compile

org.apache.httpcomponents

httpclient

io.github.openfeign

feign-okhttp

application.yml

feign:

hystrix:

enabled: true

httpclient:

enabled: true

okhttp:

enabled: true

添加对GET请求的拦截器

import com.alibaba.fastjson.JSON;

import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.ObjectMapper;

import feign.RequestInterceptor;

import feign.RequestTemplate;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.http.HttpMethod;

import org.springframework.stereotype.Component;

import org.springframework.util.StringUtils;

import javax.annotation.Resource;

import java.io.IOException;

import java.util.*;

/**

* 拦截Fegin的get请求,使之支持get请求pojo传参

*/

@Component

public class FeignRequestInterceptor implements RequestInterceptor {

private static final Logger log = LoggerFactory.getLogger(FeignRequestInterceptor.class);

@Resource

private ObjectMapper objectMapper;

@Override

public void apply(RequestTemplate template) {

if (HttpMethod.GET.name().equals(template.method())

&& null != template.body()) {

try {

JsonNode jsonNode = objectMapper.readTree(template.body());

template.body(null);

Map> queries = new HashMap<>();

buildQuery(jsonNode, "", queries);

template.queries(queries);

} catch (IOException e) {

log.error("【拦截GET请求POJO方式】-出错了:{}", JSON.toJSONString(e));

throw new RuntimeException();

}

}

}

private void buildQuery(JsonNode jsonNode, String path, Map> queries) {

// 叶子节点

if (!jsonNode.isContainerNode()) {

if (jsonNode.isNull()) {

return;

}

Collection values = queries.get(path);

if (null == values) {

values = new ArrayList<>();

queries.put(path, values);

}

values.add(jsonNode.asText());

return;

}

// 数组节点

if (jsonNode.isArray()) {

Iterator it = jsonNode.elements();

while (it.hasNext()) {

buildQuery(it.next(), path, queries);

}

} else {

Iterator> it = jsonNode.fields();

while (it.hasNext()) {

Map.Entry entry = it.next();

if (StringUtils.hasText(path)) {

buildQuery(entry.getValue(), path + "." + entry.getKey(), queries);

} else { // 根节点

buildQuery(entry.getValue(), entry.getKey(), queries);

}

}

}

}

}

其他

此时你的应用程序就可以通过POJO封装参数向其他服务发起调用了。但是如果你请求的Content-Type是application/json的,那么你要需要指定请求的consumes为consumes = MediaType.APPLICATION_JSON_VALUE。

原文出处:https://www.cnblogs.com/QullLee/p/10682719.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值