SpringCloud Feign多參數(shù)傳遞及需要注意的問題
Feign多參數(shù)傳遞及注意的問題
這邊沿用前面的Eureka,F(xiàn)eign,Service
在服務(wù)提供者cloud-shop-userservice中新增幾個(gè)方法
/**
* 保存用戶
* 2018年1月18日
*/
@PostMapping("/user")
public String aveUser(@RequestBody User user) {
logger.info("保存用戶 :" +user.toString());
return "Success";
}
/**
* 根據(jù)用戶名和密碼查詢用戶
* 2018年1月18日
*/
@GetMapping("/findUser")
public User findUserByNameAndPassword(String name ,String password) {
logger.info("name :"+name +"---password :" +password);
User user= new User();
user.setName(name);
user.setPassword(password);
return user;
}修改feign的UserService,新增對(duì)應(yīng)的方法
package cn.sh.daniel.service;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import cn.sh.daniel.entity.User;
@FeignClient(value = "cloud-shop-userservice")
public interface UserService {
@GetMapping("/user/{id}")
public User findUserById(@PathVariable("id")Long id);
@PostMapping("/user/user")
public String aveUser(@RequestBody User user) ;
@GetMapping("/user/findUser")
public User findUserByNameAndPassword(String name ,String password);
}在feign的controller中調(diào)用方法
/**
* 保存用戶
* 2018年1月18日
*/
@PostMapping("/user")
public String aveUser(@RequestBody User user) {
return userService.aveUser(user);
}
/**
* 根據(jù)用戶名和密碼查詢用戶
* 2018年1月18日
*/
@GetMapping("/findUser")
public User findUserByNameAndPassword(String name ,String password) {
return userService.findUserByNameAndPassword(name, password);
}重啟修改過的服務(wù),查看服務(wù)注冊(cè)是否正常
在啟動(dòng)過程中可以發(fā)現(xiàn)Feign服務(wù)啟動(dòng)報(bào)錯(cuò):


為什么會(huì)報(bào)錯(cuò)呢?
這個(gè)方法有兩個(gè)參數(shù),而Feign去映射的時(shí)候它不會(huì)去自動(dòng)給你區(qū)分那個(gè)參數(shù)是哪個(gè),會(huì)直接給你報(bào)錯(cuò)
解決方法:添加注解,自己去指定要映射的屬性

重新啟動(dòng)Feign服務(wù):

啟動(dòng)成功?。。?!
使用工具調(diào)用這幾個(gè)方法進(jìn)行測(cè)試



成功調(diào)用兩個(gè)方法?。。。?/p>
Feign如何接收多個(gè)參數(shù)
feigin多個(gè)參數(shù)POST情況下
method(String str1,String str2,String str3); method2(String str1,@RequestParam Map<String, Object> map,String str3);
1.API
package com.hwasee.hsc.api.redis;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Map;
/**
* @author limaojing
* @date 2020-07-28
*/
public interface RedisMapAPI {
//===============================Map===============================
@PostMapping("/redis/map/get")
String getMap(@RequestParam(value = "key") String key, @RequestParam(value = "item") String item);
@PostMapping("/redis/map/getAll")
Map<Object, Object> getAllMap(@RequestParam(value = "key") String key);
@PostMapping("/redis/map/set")
Boolean setMap(@RequestParam(value = "key") String key, @RequestParam Map<String, Object> map);
@PostMapping("/redis/map/setMapAndTime")
Boolean setMapAndTime(@RequestParam(value = "key") String key, @RequestParam Map<String, Object> map, @RequestParam(value = "time") Long time);
@PostMapping("/redis/map/setMapItem")
Boolean setMapItem(@RequestParam(value = "key") String key, @RequestParam(value = "item") String item, @RequestParam(value = "value") String value);
@PostMapping("/redis/map/setMapItemAndTime")
Boolean setMapItemAndTime(@RequestParam(value = "key") String key, @RequestParam(value = "item") String item, @RequestParam(value = "value") String value, @RequestParam(value = "time") Long time);
@PostMapping("/redis/map/del")
void delMap(@RequestParam(value = "key") String key, @RequestParam(value = "items") Object[] items);
@PostMapping("/redis/map/hashKey")
Boolean mhashKey(@RequestParam(value = "key") String key, @RequestParam(value = "item") String item);
@PostMapping("/redis/map/incr")
Double incrMap(@RequestParam(value = "key") String key, @RequestParam(value = "item") String item, @RequestParam(value = "delta") Double delta);
@PostMapping("/redis/map/decr")
Double decrMap(@RequestParam(value = "key") String key, @RequestParam(value = "item") String item, @RequestParam(value = "delta") Double delta);
}
2.Feign
package com.hwasee.hsc.feign.redis;
import com.hwasee.hsc.api.redis.RedisMapAPI;
import com.hwasee.hsc.constants.ServiceConstants;
import org.springframework.cloud.openfeign.FeignClient;
/**
* @author limaopeng
* @date 2020-11-25
*/
@FeignClient(name = ServiceConstants.Services.SERVICE_REDIS)
public interface RedisMapFeign extends RedisMapAPI {
}
3.controller
如果實(shí)現(xiàn)了API就不用添加,沒有實(shí)現(xiàn)就要添加
package com.hwasee.hsc.redis.controller;
import com.hwasee.hsc.redis.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* @author limaojing
* @date 2020-07-28
*/
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisUtil redisUtil;
@Autowired
private RedisUtil.redisMap redisMap;
@Autowired
private RedisUtil.redisString redisString;
@Autowired
private RedisUtil.redisSet redisSet;
@Autowired
private RedisUtil.redisList redisList;
//===============================Common===============================
@PostMapping("/changeDatabase")
public void changeDatabase(Integer index){
redisUtil.changeDatabase(index);
}
/**
* 指定緩存失效時(shí)間
*
* @param key 鍵
* @param time 時(shí)間(秒)
* @return
*/
@PostMapping("/expire")
public Boolean expire(String key, Long time) {
return redisUtil.expire(key, time);
}
/**
* 根據(jù)key獲取過期時(shí)間
*
* @param key 鍵,不能為空
* @return 時(shí)間秒,返回0代表永久有效
*/
@PostMapping("/getExpire")
public Long getExpire(String key) {
return redisUtil.getExpire(key);
}
/**
* 判斷key是否存在
*
* @param key 鍵
* @return 存在返回true,不存在返回false
*/
@PostMapping("/hasKey")
public Boolean hasKey(String key) {
return redisUtil.hasKey(key);
}
/**
* 刪除緩存
*
* @param keys 可以傳一個(gè)值,或多個(gè)值
*/
@SuppressWarnings("unchecked")
@PostMapping("/del")
public void del(@RequestParam String[] keys) {
redisUtil.del(keys);
}
//===============================String===============================
/**
* 獲取緩存
*
* @param key 鍵
* @return 值
*/
@PostMapping("/string/get")
public String getString(String key) {
return redisString.get(key).toString();
}
/**
* 緩存存入
*
* @param key 鍵
* @param value 值
* @return 操作成功返回true,失敗返回false
*/
@PostMapping("/string/set")
public Boolean setString(String key, String value) {
return redisString.set(key, value);
}
/**
* 普通緩存放入并設(shè)置時(shí)間
*
* @param key 鍵
* @param value 值
* @param time 時(shí)間(秒) time要大于0 如果time小于等于0 將設(shè)置無限期
* @return 操作成功返回true,失敗返回false
*/
@PostMapping("/string/setValueAndTime")
public Boolean setValueAndTime(String key, String value, Long time) {
return redisString.set(key, value, time);
}
/**
* 遞增
*
* @param key 鍵
* @param delta 要增加的值
* @return
*/
@PostMapping("/string/incr")
public Long incrString(String key, Long delta) {
return redisString.incr(key, delta);
}
/**
* 遞減
*
* @param key 鍵
* @param delta 要減小的值
* @return
*/
@PostMapping("/string/decr")
public Long decrString(String key, Long delta) {
return redisString.decr(key, delta);
}
//===============================Map===============================
/**
* 取得對(duì)應(yīng)鍵值
*
* @param key 鍵
* @param item 項(xiàng)
* @return 值
*/
@PostMapping("/map/get")
public String getMap(String key, String item) {
return redisMap.get(key, item);
}
/**
* 獲取hashKey對(duì)應(yīng)的所有鍵值
*
* @param key 鍵
* @return map形式返回鍵值對(duì)
*/
@PostMapping("/map/getAll")
public Map<Object, Object> getAllMap(String key) {
return redisMap.getAll(key);
}
/**
* 顧名思義,當(dāng)然是set值啦
*
* @param key 鍵
* @param map 對(duì)應(yīng)的多個(gè)鍵值
* @return 操作成功返回true,失敗返回false
*/
@PostMapping("/map/set")
public Boolean setMap(String key, @RequestParam Map<String, Object> map) {
return redisMap.set(key, map);
}
/**
* 加強(qiáng)版set,可設(shè)置時(shí)間
*
* @param key 鍵
* @param map 對(duì)應(yīng)的多個(gè)鍵值
* @param time 緩存失效時(shí)間
* @return 操作成功返回true,失敗返回false
*/
@PostMapping("/map/setMapAndTime")
public Boolean setMapAndTime(@RequestParam String key,@RequestParam Map<String, Object> map,@RequestParam Long time) {
return redisMap.set(key, map, time);
}
/**
* 向一張hash表中放入數(shù)據(jù),如果不存在將創(chuàng)建
*
* @param key 鍵
* @param item 項(xiàng)
* @param value 值
* @return 操作成功返回true,失敗返回false
*/
@PostMapping("/map/setMapItem")
public Boolean setMapItem(String key, String item, String value) {
return redisMap.set(key, item, value);
}
/**
* 加強(qiáng)版set,可設(shè)置時(shí)間
*
* @param key 鍵
* @param item 項(xiàng)
* @param value 值
* @param time 緩存失效時(shí)間
* @return 操作成功返回true,失敗返回false
*/
@PostMapping("/map/setMapItemAndTime")
public Boolean setMapItemAndTime(String key, String item, String value, Long time) {
return redisMap.set(key, item,value, time);
}
/**
* 刪除hash表中的值
*
* @param key 鍵,不能為空
* @param items 項(xiàng),不能為空,可以為多個(gè)
*/
@PostMapping("/map/del")
public void delMap(String key,@RequestParam Object[] items) {
redisMap.del(key, items);
}
/**
* 判斷hash表中是否存在某值
*
* @param key 鍵,不能為空
* @param item 項(xiàng),不能為空
* @return 存在返回true,不存在返回false
*/
@PostMapping("/map/hashKey")
public Boolean mhashKey(String key, String item) {
return redisMap.hasKey(key, item);
}
/**
* hash遞增
*
* @param key
* @param item
* @param delta 要增加多少
* @return
*/
@PostMapping("/map/incr")
public Double incrMap(String key, String item, Double delta) {
return redisMap.incr(key, item, delta);
}
/**
* hash遞減
*
* @param key
* @param item
* @param delta 要減少多少
* @return
*/
@PostMapping("/map/decr")
public Double decrMap(String key, String item, Double delta) {
return redisMap.decr(key, item, delta);
}
//===============================Set===============================
/**
* 根據(jù)key獲取Set中的所有值
*
* @param key
* @return
*/
@PostMapping("/set/get")
public Set<Object> getSet(String key) {
return redisSet.get(key);
}
/**
* 將數(shù)據(jù)放入set緩存
*
* @param key
* @param values
* @return 成功個(gè)數(shù)
*/
@PostMapping("/set/setValue")
public Long setValue(String key,@RequestParam Object[] values) {
return redisSet.set(key, values);
}
/**
* 根據(jù)value從一個(gè)set中查詢,是否存在
*
* @param key
* @param value
* @return 存在返回true,不存在返回false
*/
@PostMapping("/set/hashKey")
public Boolean hashKey(String key, String value) {
return redisSet.hasKey(key, value);
}
/**
* 將數(shù)據(jù)放入set緩存,可設(shè)置時(shí)間
*
* @param key
* @param time
* @param values
* @return 成功個(gè)數(shù)
*/
@PostMapping("/set/setValueAndTime")
public Long setValueAndTime(String key, Long time,@RequestParam Object[] values) {
return redisSet.set(key, time, values);
}
/**
* 獲取set緩存的長(zhǎng)度
*
* @param key
* @return
*/
@PostMapping("/set/getSize")
public Long getSize(String key) {
return redisSet.getSize(key);
}
/**
* 移除set中值為value的項(xiàng)
*
* @param key
* @param values
* @return 移除的個(gè)數(shù)
*/
@PostMapping("/set/remove")
public Long remove(String key,@RequestParam Object[] values) {
return redisSet.remove(key, values);
}
//===============================List===============================
/**
* 獲取list緩存的內(nèi)容
*
* @param key
* @param start
* @param end 0到結(jié)束,-1代表所有值
* @return
*/
@PostMapping("/list/get")
public List<Object> get(String key, Long start, Long end) {
return redisList.get(key, start, end);
}
/**
* 獲取list緩存的長(zhǎng)度
*
* @param key
* @return
*/
@PostMapping("/list/getSize")
public Long getListSize(String key) {
return redisList.getSize(key);
}
/**
* 通過索引 獲取list中的值
*
* @param key
* @param index 索引 index>=0時(shí), 0 表頭,1 第二個(gè)元素,依次類推;index<0時(shí),-1,表尾,-2倒數(shù)第二個(gè)元素,依次類推
* @return
*/
@PostMapping("/list/getByIndex")
public Object getByIndex(@RequestParam("key") String key, @RequestParam("index") Long index) {
return redisList.getByIndex(key, index);
}
/**
* 將list放入緩存
*
* @param key
* @param value
* @return
*/
@PostMapping("/list/setValue")
public Boolean setValue(String key, Object value) {
return redisList.set(key, value);
}
/**
* 將list放入緩存,可設(shè)置時(shí)間
*
* @param key
* @param value
* @param time
* @return
*/
@PostMapping("/list/setValueAndTime")
public Boolean setValueAndTime(String key, Object value, Long time) {
return redisList.set(key, value, time);
}
/**
* 將list放入緩存
*
* @param key
* @param value
* @return
*/
@PostMapping("/list/setList")
public Boolean setList(String key, List<Object> value) {
return redisList.set(key, value);
}
/**
* 將list放入緩存,可設(shè)置時(shí)間
*
* @param key
* @param value
* @param time
* @return
*/
@PostMapping("/list/setListAndTime")
public Boolean setListAndTime(String key, List<Object> value, Long time) {
return redisList.set(key, value, time);
}
/**
* 根據(jù)索引修改list中的某條數(shù)據(jù)
*
* @param key
* @param index
* @param value
* @return
*/
@PostMapping("/list/updateByIndex")
public Boolean updateByIndex(String key, Long index, Object value) {
return redisList.updateIndex(key, index, value);
}
/**
* 刪除list中值為value的項(xiàng)
*
* @param key 鍵
* @param count 要移除的個(gè)數(shù)
* @param value
* @return 移除的個(gè)數(shù)
*/
@PostMapping("/list/remove")
public Long remove(String key, Long count, Object value) {
return redisList.remove(key, count, value);
}
}
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA運(yùn)行SpringBoot項(xiàng)目的詳細(xì)步驟(圖文教程)
本文主要介紹了IDEA運(yùn)行SpringBoot項(xiàng)目的詳細(xì)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
SpringBoot在啟動(dòng)類main方法中調(diào)用service層方法報(bào)“空指針異?!暗慕鉀Q辦法
這篇文章主要介紹了SpringBoot在啟動(dòng)類main方法中調(diào)用service層方法報(bào)“空指針異常“的解決辦法,大多數(shù)情況下,我們使用Springboot是創(chuàng)建一個(gè)maven項(xiàng)目,然后通過controller層的接口調(diào)用,但也有特殊情況,文章介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06
Java將CSV的數(shù)據(jù)發(fā)送到kafka的示例
這篇文章主要介紹了Java將CSV的數(shù)據(jù)發(fā)送到kafka得示例,幫助大家更好得理解和使用Java,感興趣的朋友可以了解下2020-11-11
java實(shí)現(xiàn)百度云OCR文字識(shí)別 高精度OCR識(shí)別身份證信息
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)百度云OCR文字識(shí)別,高精度OCR識(shí)別身份證信息,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
Java?C++算法題解leetcode801使序列遞增的最小交換次數(shù)
這篇文章主要為大家介紹了Java?C++題解leetcode801使序列遞增的最小交換次數(shù)示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
plsql實(shí)現(xiàn)DES對(duì)稱加密 Java解密
這篇文章主要介紹了plsql實(shí)現(xiàn)DES對(duì)稱加密 Java解密的方法,幫助大家更好的理解和學(xué)習(xí)使用Oracle與Java,感興趣的朋友可以了解下2021-02-02
java中你的項(xiàng)目應(yīng)該如何正確分層
這篇文章主要介紹了java中你的項(xiàng)目應(yīng)該如何正確分層,業(yè)務(wù)分層對(duì)于代碼規(guī)范是比較重要,決定著以后的代碼是否可復(fù)用,感興趣的可以了解一下2021-04-04
解決SpringBoot項(xiàng)目啟動(dòng)成功但接口訪問404的問題
這篇文章主要介紹了如何解決SpringBoot項(xiàng)目啟動(dòng)成功但接口訪問404的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,接下來就跟著小編一起來看看吧2023-07-07

