OpenFeign发送请求时传多个参数时的写法

1. 多参数的Controller接口Get方法

package com.demo.product.controller;

import com.demo.handler.AjaxResult;
import com.demo.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * author:HUAWEI
 */
@RestController
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/reduceStock")
    AjaxResult reduceStock(@RequestParam("productId") Long productId, @RequestParam("amount")Integer amount)
    {
        Double remain = productService.reduceStock(productId, amount);
        return AjaxResult.success(remain);
    }

}

2. 使用@RequestParam指定多个参数的get方法

package com.demo.order.feign.service;

import com.demo.entity.AjaxResult;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * author:HUAWEI
 */
@FeignClient(name = "product-seata")
public interface ProductFeignService {

    /**
     * 扣减库存
     *
     * @param productId 商品 ID
     * @param amount 扣减数量
     * @return 商品总价
     */
    @GetMapping(value = "/reduceStock")
    AjaxResult reduceStock(@RequestParam("productId")Long productId, @RequestParam("amount")Integer amount);
}

3. 多参数的Controller接口Post方法

package com.demo.controller;

import com.demo.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * author:HUAWEI
 */
@RestController
public class AccountController {

    @Autowired
    private AccountService accountService;

    @PostMapping("/reduceBalance")
    void reduceBalance(@RequestParam("userId") Long userId, @RequestParam("price")Double price)
    {
        accountService.reduceBalance(userId, price);
    }


}

4. OpenFeign接口

package com.demo.order.feign.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * author:HUAWEI
 */
@FeignClient(name = "account-seata")
public interface AccountFeignService {

    /**
     * 账户扣减
     * @param userId 用户 ID
     * @param price 扣减金额
     */
    @PostMapping(value = "/reduceBalance")
    void reduceBalance(@RequestParam("userId")Long userId, @RequestParam("price")Double price);
}

5. @RequstBody形式的参数请求接口

@RequestMapping("getToken")
	@ResponseBody	
	public String getToken(HttpServletRequest request, @RequestBody GetTokenRequest tokenRequest) throws Exception
	{
       return "";
    }

6. @RequstBody形式的OpenFeign接口声明

@FeignClient(name = "ftsafe-authserver")
public interface GetTokenService {
	
	@GetMapping(value = "/getToken")
    public String getTokenInfo(@RequestBody GetTokenRequest tokenRequest);

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值