SpringCloud Feign多參數(shù)傳遞及需要注意的問題
Feign多參數(shù)傳遞及注意的問題
這邊沿用前面的Eureka,F(xiàn)eign,Service
在服務(wù)提供者cloud-shop-userservice中新增幾個方法
/**
* 保存用戶
* 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,新增對應(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ù)注冊是否正常
在啟動過程中可以發(fā)現(xiàn)Feign服務(wù)啟動報錯:


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

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

啟動成功?。。?!
使用工具調(diào)用這幾個方法進行測試



成功調(diào)用兩個方法!?。?!
Feign如何接收多個參數(shù)
feigin多個參數(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
如果實現(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);
}
/**
* 顧名思義,當(dāng)然是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);
}
}
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java string類型轉(zhuǎn)換boolean類型的方法
下面小編就為大家?guī)硪黄猨ava string類型轉(zhuǎn)換boolean類型的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11
java中CompletableFuture異步執(zhí)行方法
本文主要介紹了java中CompletableFuture異步執(zhí)行方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
SpringBoot獲取當(dāng)前運行環(huán)境三種方式小結(jié)
在使用SpringBoot過程中,我們只需要引入相關(guān)依賴,然后在main方法中調(diào)用SpringBootApplication.run(應(yīng)用程序啟動類.class)方法即可,那么SpringBoot是如何獲取當(dāng)前運行環(huán)境呢,接下來由小編給大家介紹一下SpringBoot獲取當(dāng)前運行環(huán)境三種方式,需要的朋友可以參考下2024-01-01
java的MybatisPlus調(diào)用儲存過程的返回數(shù)據(jù)問題
這篇文章主要介紹了java的MybatisPlus調(diào)用儲存過程的返回數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
IDEA 單元測試報錯:Class not found:xxxx springb
這篇文章主要介紹了IDEA 單元測試報錯:Class not found:xxxx springboot的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01

