您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“SpringCloud Feign多參數(shù)傳遞的方法及需要注意哪些問題”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“SpringCloud Feign多參數(shù)傳遞的方法及需要注意哪些問題”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識吧。
這邊沿用前面的Eureka,F(xiàn)eign,Service
/** * 保存用戶 * 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; }
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); }
/** * 保存用戶 * 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); }
在啟動過程中可以發(fā)現(xiàn)Feign服務(wù)啟動報錯:
為什么會報錯呢?
這個方法有兩個參數(shù),而Feign去映射的時候它不會去自動給你區(qū)分那個參數(shù)是哪個,會直接給你報錯
解決方法:添加注解,自己去指定要映射的屬性
重新啟動Feign服務(wù):
啟動成功?。。。?/p>
成功調(diào)用兩個方法?。。。?/p>
feigin多個參數(shù)POST情況下
method(String str1,String str2,String str3); method2(String str1,@RequestParam Map<String, Object> map,String str3);
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); }
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 { }
如果實現(xiàn)了API就不用添加,沒有實現(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); } /** * 指定緩存失效時間 * * @param key 鍵 * @param time 時間(秒) * @return */ @PostMapping("/expire") public Boolean expire(String key, Long time) { return redisUtil.expire(key, time); } /** * 根據(jù)key獲取過期時間 * * @param key 鍵,不能為空 * @return 時間秒,返回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 可以傳一個值,或多個值 */ @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è)置時間 * * @param key 鍵 * @param value 值 * @param time 時間(秒) 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=============================== /** * 取得對應(yīng)鍵值 * * @param key 鍵 * @param item 項 * @return 值 */ @PostMapping("/map/get") public String getMap(String key, String item) { return redisMap.get(key, item); } /** * 獲取hashKey對應(yīng)的所有鍵值 * * @param key 鍵 * @return map形式返回鍵值對 */ @PostMapping("/map/getAll") public Map<Object, Object> getAllMap(String key) { return redisMap.getAll(key); } /** * 顧名思義,當然是set值啦 * * @param key 鍵 * @param map 對應(yīng)的多個鍵值 * @return 操作成功返回true,失敗返回false */ @PostMapping("/map/set") public Boolean setMap(String key, @RequestParam Map<String, Object> map) { return redisMap.set(key, map); } /** * 加強版set,可設(shè)置時間 * * @param key 鍵 * @param map 對應(yīng)的多個鍵值 * @param time 緩存失效時間 * @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 項 * @param value 值 * @return 操作成功返回true,失敗返回false */ @PostMapping("/map/setMapItem") public Boolean setMapItem(String key, String item, String value) { return redisMap.set(key, item, value); } /** * 加強版set,可設(shè)置時間 * * @param key 鍵 * @param item 項 * @param value 值 * @param time 緩存失效時間 * @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 項,不能為空,可以為多個 */ @PostMapping("/map/del") public void delMap(String key,@RequestParam Object[] items) { redisMap.del(key, items); } /** * 判斷hash表中是否存在某值 * * @param key 鍵,不能為空 * @param item 項,不能為空 * @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 成功個數(shù) */ @PostMapping("/set/setValue") public Long setValue(String key,@RequestParam Object[] values) { return redisSet.set(key, values); } /** * 根據(jù)value從一個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è)置時間 * * @param key * @param time * @param values * @return 成功個數(shù) */ @PostMapping("/set/setValueAndTime") public Long setValueAndTime(String key, Long time,@RequestParam Object[] values) { return redisSet.set(key, time, values); } /** * 獲取set緩存的長度 * * @param key * @return */ @PostMapping("/set/getSize") public Long getSize(String key) { return redisSet.getSize(key); } /** * 移除set中值為value的項 * * @param key * @param values * @return 移除的個數(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緩存的長度 * * @param key * @return */ @PostMapping("/list/getSize") public Long getListSize(String key) { return redisList.getSize(key); } /** * 通過索引 獲取list中的值 * * @param key * @param index 索引 index>=0時, 0 表頭,1 第二個元素,依次類推;index<0時,-1,表尾,-2倒數(shù)第二個元素,依次類推 * @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è)置時間 * * @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è)置時間 * * @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的項 * * @param key 鍵 * @param count 要移除的個數(shù) * @param value * @return 移除的個數(shù) */ @PostMapping("/list/remove") public Long remove(String key, Long count, Object value) { return redisList.remove(key, count, value); } }
讀到這里,這篇“SpringCloud Feign多參數(shù)傳遞的方法及需要注意哪些問題”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領(lǐng)會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。