首页 > 其他 > 详细

feignclient发送get请求,传递参数为对象

时间:2020-02-28 21:12:07      阅读:786      评论:0      收藏:0      [点我收藏+]

feignclient发送get请求,传递参数为对象。此时不能使用在地址栏传递参数的方式,需要将参数放到请求体中。

  • 第一步:

修改application.yml中配置feign发送请求使用apache httpclient 而不是默认的jdk UrlConnection

feign.httpclient.enabled= true
  • 第二步:

pom.xml中增加对apache httpclient的支持。

<!-- 配置feign 发送请求使用 httpclient,而不是java原生 -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
		</dependency>
		<!-- 使用Apache HttpClient替换Feign原生httpclient -->
		<dependency>
			<groupId>com.netflix.feign</groupId>
			<artifactId>feign-httpclient</artifactId>
			<version>8.15.1</version>
		</dependency>
  • 第三步:编写接口类

在ApacheHttpClient中看到默认设置的Content-Type是ContentType.DEFAULT_TEXT,即text/plain; charset=ISO-8859-1, 此时传递对象需要配置为application/json

@FeignClient(name="feign-consumer")
public interface ServiceClient {

    /**@param user
     * @return
     */
    @RequestMapping(method = RequestMethod.GET, value = "/test4",consumes="application/json")
    String getInstanceInfo4(User user);

}
  • 第四步:编写接收请求类
	/**Feign发送Get请求时用对象传递参数
	 * @param servers
	 * @return
	 */
	@RequestMapping(value="/test4", method = RequestMethod.GET,consumes="application/json")  
	public String firstDemo4(@RequestBody User u) {
		
		System.out.println(u);
		return "hello3"+u;
	}  

  参考链接:

    https://blog.csdn.net/cuiyaoqiang/article/details/81215483

 

上手,运行。。。报错:Caused by: java.lang.NoSuchMethodError: feign.Response.create(ILjava/lang/String;Ljava/util/Map;Lfeign/Response$Body;)Lfeign/Response;

  参考 https://ask.csdn.net/questions/773444 这个问答,得知需要修改依赖版本

<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-httpclient</artifactId>
    <version>10.1.0</version>
</dependency>

  完美解决

 

feignclient发送get请求,传递参数为对象

原文:https://www.cnblogs.com/wwct/p/12379127.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!