好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

解决feignClient调用时获取返回对象类型匹配的问题

feignClient调用时获取返回对象类型匹配

feignClient是springCloud体系中重要的一个组件,用于微服务之间的相互调用,底层为httpClient,在之前的应用中,我一直以为A服务提供的方法返回类型为对象的话,那么调用A服务的B服务必须也用字段类型以及命名完全相同的对象来接收,为此我验证了一下,发现不是必须用完全相同的对象来接收,比如,可以用map<String,Object>或者Object来接收,然后解析。

当然,复杂对象我还是推荐用一个完全相同的对象来接收。

下面是我的例子:

feignClient是springCloud体系中重要的一个组件,用于微服务之间的相互调用,底层为httpClient,在之前的应用中,我一直以为A服务提供的方法返回类型为对象的话,那么调用A服务的B服务必须也用字段类型以及命名完全相同的对象来接收,为此我验证了一下,发现不是必须用完全相同的对象来接收,比如,可以用map<String,Object>或者Object来接收,然后解析。

当然,复杂对象我还是推荐用一个完全相同的对象来接收。

下面是我的例子:

项目一:首先创建一个服务注册中心eureka

配置文件

项目二:紧接着创建一个服务提供方eureka-client,方法helloWorld返回值为map

注册到eureka中

项目三:创建服务调用方service-feign

注意:map的key与Hello类字段属性对应

注册到eureka

最后:启动三个项目

发现项目二 项目三已经被注册到服务中心

调用项目三的接口

可以正常返回,由于项目二返回类型是map.而项目三是用对象Hello来接收的,那么就说明了服务提供方的返回值类型和调用方接收值类型并不是需要完全对应的。

feignClient传参(参数为对象类型)的一个坑

客户端

@RequestMapping ( value = "/friendCircleComment/comment" , method = RequestMethod . POST ) R comment ( @RequestBody FriendCircleComment friendCircleComment );

服务端

@RequestMapping ( value = "/comment" ) public R comment ( @RequestBody FriendCircleComment friendCircleComment ){ friendCircleCommentService . comment ( friendCircleComment ); return new R (); }

这么传参是没问题的,服务端也能接收到

但是,问题来了,

小程序的post请求的header必须为

header :{ 'content-type' : 'application/x-www-form-urlencoded' },

导致后台为@RequestBody接收不到参数,

feignClient默认参数请求类型是

header :{ 'content-type' : 'application/json' },

定义@RequestBody接收参数的headers类型必须为

header :{ 'content-type' : 'application/json' },

所以这样就有冲突,feignClient和定义为'content-type':'application/x-www-form-urlencoded'的请求接口不能共用

解决方法

不使用对象接收,使用基本类型接收

如下

客户端

@RequestMapping ( value = "/friendCircleComment/comment" , method = RequestMethod . POST ) R comment ( @RequestParam ( value = "friendCircleId" , required = false ) Integer friendCircleId , @RequestParam ( value = "memberId" , required = false ) Integer memberId , @RequestParam ( value = "parentId" , required = false ) Integer parentId , @RequestParam ( value = "comment" , required = false ) String comment , @RequestParam ( value = "replyMemberId" , required = false ) Integer replyMemberId );

服务端

@RequestMapping ( value = "/comment" ) public R comment ( @RequestParam ( value = "friendCircleId" , required = false ) Integer friendCircleId , @RequestParam ( value = "memberId" , required = false ) Integer memberId , @RequestParam ( value = "parentId" , required = false ) Integer parentId , @RequestParam ( value = "comment" , required = false ) String comment , @RequestParam ( value = "replyMemberId" , required = false ) Integer replyMemberId ){ FriendCircleComment friendCircleComment = new FriendCircleComment (); friendCircleComment . setFriendCircleId ( friendCircleId ); friendCircleComment . setMemberId ( memberId ); friendCircleComment . setParentId ( parentId ); friendCircleComment . setComment ( comment ); friendCircleComment . setReplyMemberId ( replyMemberId ); friendCircleCommentService . comment ( friendCircleComment ); return new R (); }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

原文链接:https://blog.csdn.net/Huangcsdnjava/article/details/80642672

查看更多关于解决feignClient调用时获取返回对象类型匹配的问题的详细内容...

  阅读:23次