How does SpringCloud use complex parameter passing when feign
- 2021-10-25 06:46:53
- OfStack
Note of Feign reference transmission
Recently, I tried to reconstruct the previous project with SpringCloud, and used Feign client components to call micro-services. There is often a problem that parameters cannot be transferred to null. After checking on the Internet for 1 time, I found that there are still 1 fixed restrictions on the use of feign parameters, mainly to pay attention to:
1. When the parameters are complex, feign will force post requests even if declared as get requests
2. @ GetMapping similar annotation declaration request is not supported, you need to use
@RequestMapping(value = "url",method = RequestMethod.GET)
3. The @ RequestParam annotation must be followed by the parameter name
I wrote a simple case, and transmitted an object and a string as request parameters at the same time, focusing on the declaration of parameters. Please refer to the rest, such as the dependency, configuration and subsequent use of eureka and feign. For reference only
Server (producer)
Prepare:
1. The service has been configured to register with eureka, and the service name is "item-service"
2. The service and dao layers are already implemented
Logic: ItemController receives parameters, calls service layer, adds details desc to commodity Item object in service, then calls dao to save Item object, and returns Result result encapsulation object
@RestController
public class ItemController {
@Autowired
private ItemService itemService;
/**
* Add
* @param item
* @param desc
* @return
*/
@RequestMapping("/item/save")
public Result addItem(@RequestBody Item item, @RequestParam("desc") String desc){
return itemService.addItem(item, desc);
}
}
Client (consumer)
Prepare: Register with eureka has been configured
Logic:
Declare that ItemFeignClient calls a service named "item-service" on eureka and returns an Result object
Using the Post request, pass two parameters:
1. The TbItem object, declared using @ RequestBody
2. The String string, declared using @ RequestParam ("xxx")
@FeignClient("item-service")
public interface ItemFeignClient {
/**
* Add
* @param item
* @param desc
* @return
*/
@RequestMapping(value = "/item/save",method = RequestMethod.POST)
Result addItem(@RequestBody TbItem item, @RequestParam("desc") String desc);
SpringCloud feign Parameter Transfer Problem Record
Object passing @ RequestBody (required = false)
Multi-object delivery is not supported, at least I find it so far. Please put forward any mistakes or improved methods
API layer
@FeignClient(value = "transaction-feign")
public interface TransactionApiService {
/**
* Transaction flow query Paginable
*
* @param param
* @param pageNum
* @param pageSize
* @return
*/
@RequestMapping(value = "/dealflow/list", method = RequestMethod.POST)
JsonResultDO dealFlowList(@RequestBody(required = false) DealFlowDo param
, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
, @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize);
}
Interface layer
/**
* Transaction flow query Paginable
* @return JsonResultDO
*/
@RequestMapping(value = "/dealflow/list", method = RequestMethod.POST)
public Page<DealFlowDo> dealFlowList(@RequestBody(required = false) DealFlowDo param
, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum
, @RequestParam(value = "pageSize", defaultValue = "20") Integer pageSize
, @RequestParam(value = "userId") Long userId){
JsonResultDO resultDO = new JsonResultDO();
Page<DealFlowDo> page = (Page<DealFlowDo> )dealFlowService.queryDealFlowList(userId, param, pageNum, pageSize);
return page;
}
Single or multiple parameter passing @ RequestParam/@ PathVariable/***
/**
* Obtain merchant information according to merchant number
*
* @param merNo
* @return
*/
@RequestMapping(value = "/merchant/{merNo}", method = RequestMethod.GET)
String getMerchantByMerNo(@PathVariable("merNo") String merNo);