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);
}