feign调用参数传不过去,用feign请求参数不对
e * @return */ @GetMapping("/get/01") String queryDataByGetRequest(@RequestParam("startTime") Date startTime, @RequestParam("endTime") Date endTime, @RequestParam("message") String message);}服务提供方:
@RestControllerpublic class RequestProviderController { @GetMapping("/get/01") public String queryDataByGetRequest(@RequestParam("startTime") Date startTime, @RequestParam("endTime") Date endTime, @RequestParam("message") String message) { Requets01DTO requets01DTO = new Requets01DTO(); requets01DTO.setStartTime(startTime); requets01DTO.setEndTime(endTime); requets01DTO.setMessage(message); return requets01DTO.toString(); }}
请求结果截图:
GET方式请求②
Feign调用的请求改为POST方式
消费方的请求:
@Autowired GetClient02 getClient02; @GetMapping("/request/get/02") public String requestGetTwo(Requets01DTO requets01DTO) { return getClient02.queryDataByGetRequest(requets01DTO); }
Feign接口:
package com.springcloud.service; import com.springcloud.pojo.Requets01DTO;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.PostMapping; @FeignClient(value = "provider-8762", contextId = "GetClient02")public interface GetClient02 { /** * GET方式请求②(Feign调用的请求改为POST方式) * * @param requets01DTO * @return */ @PostMapping("/post/02") String queryDataByGetRequest(Requets01DTO requets01DTO);}
服务提供方:
@PostMapping("/post/02") public String queryDataByGetRequest(@RequestBody Requets01DTO requets01DTO) { return requets01DTO.toString(); }
请求结果截图:
GET方式请求③
组装Feign接口请求时,将body里面的参数取出来,转换为GET方式请求的参数
添加Feign请求的配置类:
package com.springcloud.config; import com.alibaba.fastjson.JSON;import feign.Request;import feign.RequestInterceptor;import feign.RequestTemplate;import org.springframework.context.annotation.Configuration;import org.springframework.http.HttpMethod;import org.springframework.web.context.request.RequestContextHolder;import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest;import java.lang.reflect.Field;import java.util.*; @Configurationpublic class FeignConfiguration implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = attributes.getRequest(); //填充get中的body数据转化成query数据 if (requestTemplate.method().equals(HttpMethod.GET.name()) && Objects.nonNull(requestTemplate.body())) { String json = requestTemplate.requestBody().asString(); Map<String, Object> map = JSON.parseObject(json); Set<String> set = map.keySet(); Iterator<String> it = set.iterator(); while (it.hasNext()) { String key = it.next(); Object values = map.get(key); if (Objects.nonNull(values)) { // 将body的参数写入queries requestTemplate.query(key, values.toString()); } } try{ Class requestClass = requestTemplate.getClass(); Field field = requestClass.getDeclaredField("body"); field.setAccessible(true); //修改body为空。 field.set(requestTemplate, Request.Body.empty()); } catch (Exception ex) { System.out.println(ex.fillInStackTrace()); } } Enumeration<String> headerNames = request.getHeaderNames(); if (headerNames != null) { while (headerNames.hasMoreElements()) { String name = headerNames.nextElement(); String values = request.getHeader(name); // 跳过 content-length if (name.equals("content-length")){ continue; } requestTemplate.header(name, values); } } else { System.out.println(String.format("feign interceptor error header:%s", requestTemplate)); } }}
添加fastJson的maven依赖:
<!-- JSON 解析器和生成器 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.74</version> </dependency>
消费方的请求:
@Autowired GetClient03 getClient03; @GetMapping("/request/get/03") public String requestGetThree(Requets01DTO requets01DTO) { return getClient03.queryDataByGetRequest(requets01DTO); }
Feign接口:
package com.springcloud.service; import com.springcloud.config.FeignConfiguration;import com.springcloud.pojo.Requets01DTO;import org.springframework.cloud.openfeign.FeignClient;import org.springframework.web.bind.annotation.GetMapping; @FeignClient(value = "provider-8762", contextId = "GetClient03", configuration = FeignConfiguration.class)public interface GetClient03 { /** * GET方式请求③(组装Feign接口请求时,将body里面的参数取出来,转换为GET方式请求的参数) * * @param requets01DTO * @return */ @GetMapping("/get/03") String queryDataByGetRequest(Requets01DTO requets01DTO);}
服务提供方:
@GetMapping("/get/03") public String queryDataByGetRequest03(Requets01DTO requets01DTO) { return requets01DTO.toString(); }
请求结果截图:
Feign调用传递的GET参数日期,要指定jsonFormat注解,body转GET参数时,日期参数会变为字符串,需要指定日期格式
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date startTime; @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") private Date endTime;
**结果截图:**
以上为个人经验,希望能给大家一个参考,也希望大家多多支持盛行IT。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。