热门系列:


1.问题

今天在和第三方联调接口时,发现这么一个问题。先上代码:

package com.xxxx.web.xxxx.controller;

import com.xxxx.xxxx.constants.UrlConstants;
import com.xxxx.common.model.Result;
import com.xxxx.sys.bean.xxxx.NotifyCallBackDTO;
import com.xxxx.sys.bean.xxxx.UserAccessInfoDTO;
import com.xxxx.sys.service.MemberCenterService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping(value = "/memberCenter")
public class xxxMemberCenterController {

    @Resource
    private MemberCenterService memberCenterService;

    @PostMapping(value = "/payNotify")
    Result payNotify(NotifyCallBackDTO notifyCallBackDTO){
        return memberCenterService.payNotify(notifyCallBackDTO);
    }

}

这个payNotify方法,是我们供第三方调用的一个入口方法。而具体实现memberCenterService.payNotify,其实是通过Feign调用另一个服务的方法。方法接口如下:

package com.xxxx.sys.service;

import com.xxxx.common.model.Result;
import com.xxxx.sys.bean.xxxx.NotifyCallBackDTO;
import com.xxxx.sys.bean.xxxx.UserAccessInfoDTO;
import com.xxxx.sys.hystrix.MemberCenterServiceHystrixImpl;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@FeignClient(value="xxxx-xxxx-sys-service",fallback = xxxx.class)
public interface MemberCenterService {

    String BASIC_URL = "/xxxx";

    @RequestMapping(value = BASIC_URL + "/payNotify",method = RequestMethod.POST)
    Result payNotify(NotifyCallBackDTO notifyCallBackDTO);
}

那么此时,问题就来了。在xxxMemberCenterController 入口中,第三方请求过来时,NotifyCallBackDTO 对象入参是可以接收到参数数据的。但是,在实际调用下面MemberCenterService 中的接口payNotify时,入参拿到的数据都是Null。这个就很奇怪了。。。


2.解决办法

一开始,我以为是因为我的接口没有指定请求参数类型,于是在接口上加上了:

consumes = "application/x-www-form-urlencoded"

但是,测试后并不可行。问题是出现在入口通过Feign调用时丢失了数据,于是我尝试在实现接口MemberCenterService的方法payNotify中,将参数前加上@RequestBody。如下:

package com.xxxx.sys.service;

import com.xxxx.common.model.Result;
import com.xxxx.sys.bean.xxxx.NotifyCallBackDTO;
import com.xxxx.sys.bean.xxxx.UserAccessInfoDTO;
import com.xxxx.sys.hystrix.MemberCenterServiceHystrixImpl;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


@FeignClient(value="xxxx-xxxx-sys-service",fallback = xxxx.class)
public interface MemberCenterService {

    String BASIC_URL = "/xxxx";

    @RequestMapping(value = BASIC_URL + "/payNotify",method = RequestMethod.POST)
    Result payNotify(@RequestBody NotifyCallBackDTO notifyCallBackDTO);
}

在具体实现时,也加上@RequestBody。如此,数据得到全部正常接收。

 

3.总结

中间有百度过部分帖子,有的帖子建议将对象拆分为单个的字符串来一个个对应接收,但是我嫌麻烦,没有尝试。有兴趣的朋友可以尝试一下。此处个人总结,应该是Feign调用时,需要指定@RequestBody来实现对象参数传递。不对之处,请各位看官及大牛,不吝拍砖,谢谢!!

 

本博客皆为学习、分享、探讨为本,欢迎各位朋友评论、点赞、收藏、关注,一起加油!

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐