天天看點

feign接收多個參數

feigin多個參數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

(如果實作了API就不用添加,沒有實作就要添加)

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

    /**
     * 根據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);
    }

    /**
     * 普通緩存放入并設定時間
     *
     * @param key   鍵
     * @param value 值
     * @param time  時間(秒) time要大于0 如果time小于等于0 将設定無限期
     * @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===============================

    /**
     * 取得對應鍵值
     *
     * @param key  鍵
     * @param item 項
     * @return 值
     */
    @PostMapping("/map/get")
    public String getMap(String key, String item) {
        return redisMap.get(key, item);
    }

    /**
     * 擷取hashKey對應的所有鍵值
     *
     * @param key 鍵
     * @return map形式傳回鍵值對
     */
    @PostMapping("/map/getAll")
    public Map<Object, Object> getAllMap(String key) {
        return redisMap.getAll(key);
    }

    /**
     * 顧名思義,當然是set值啦
     *
     * @param key 鍵
     * @param map 對應的多個鍵值
     * @return 操作成功傳回true,失敗傳回false
     */
    @PostMapping("/map/set")
    public Boolean setMap(String key, @RequestParam Map<String, Object> map) {
        return redisMap.set(key, map);
    }

    /**
     * 加強版set,可設定時間
     *
     * @param key  鍵
     * @param map  對應的多個鍵值
     * @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表中放入資料,如果不存在将建立
     *
     * @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,可設定時間
     *
     * @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===============================

    /**
     * 根據key擷取Set中的所有值
     *
     * @param key
     * @return
     */
    @PostMapping("/set/get")
    public Set<Object> getSet(String key) {
        return redisSet.get(key);
    }

    /**
     * 将資料放入set緩存
     *
     * @param key
     * @param values
     * @return 成功個數
     */
    @PostMapping("/set/setValue")
    public Long setValue(String key,@RequestParam Object[] values) {
        return redisSet.set(key, values);
    }

    /**
     * 根據value從一個set中查詢,是否存在
     *
     * @param key
     * @param value
     * @return 存在傳回true,不存在傳回false
     */
    @PostMapping("/set/hashKey")
    public Boolean hashKey(String key, String value) {
        return redisSet.hasKey(key, value);
    }

    /**
     * 将資料放入set緩存,可設定時間
     *
     * @param key
     * @param time
     * @param values
     * @return 成功個數
     */
    @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 移除的個數
     */
    @PostMapping("/set/remove")
    public Long remove(String key,@RequestParam Object[] values) {
        return redisSet.remove(key, values);
    }

    //===============================List===============================

    /**
     * 擷取list緩存的内容
     *
     * @param key
     * @param start
     * @param end   0到結束,-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倒數第二個元素,依次類推
     * @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放入緩存,可設定時間
     *
     * @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放入緩存,可設定時間
     *
     * @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);
    }

    /**
     * 根據索引修改list中的某條資料
     *
     * @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 要移除的個數
     * @param value
     * @return 移除的個數
     */
    @PostMapping("/list/remove")
    public Long remove(String key, Long count, Object value) {
        return redisList.remove(key, count, value);
    }
}