SpringCloud Feign多参数传递及需要注意的问题
作者:潜水打豆豆 发布时间:2022-05-25 11:11:58
标签:SpringCloud,Feign,参数,传递
Feign多参数传递及注意的问题
这边沿用前面的Eureka,Feign,Service
在服务提供者cloud-shop-userservice中新增几个方法
/**
* 保存用户
* 2018年1月18日
*/
@PostMapping("/user")
public String aveUser(@RequestBody User user) {
logger.info("保存用户 :" +user.toString());
return "Success";
}
/**
* 根据用户名和密码查询用户
* 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,新增对应的方法
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中调用方法
/**
* 保存用户
* 2018年1月18日
*/
@PostMapping("/user")
public String aveUser(@RequestBody User user) {
return userService.aveUser(user);
}
/**
* 根据用户名和密码查询用户
* 2018年1月18日
*/
@GetMapping("/findUser")
public User findUserByNameAndPassword(String name ,String password) {
return userService.findUserByNameAndPassword(name, password);
}
重启修改过的服务,查看服务注册是否正常
在启动过程中可以发现Feign服务启动报错:
为什么会报错呢?
这个方法有两个参数,而Feign去映射的时候它不会去自动给你区分那个参数是哪个,会直接给你报错
解决方法:添加注解,自己去指定要映射的属性
重新启动Feign服务:
启动成功!!!!
使用工具调用这几个方法进行测试
成功调用两个方法!!!!
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);
}
}
来源:https://blog.csdn.net/yang920106/article/details/79095249
0
投稿
猜你喜欢
- 类加载器类加载器作用负责将.class文件(存储的物理文件)加载到内存中类加载器过程加载:通过全类名获取这个类准备用流传输,加载进内存,加载
- C#将对象序列化成JSON字符串public string GetJsonString() { List<Product> p
- 免责声明:本教程所有资源均来源于网络;仅用于学习交流,请勿用于任何商业行为;如需要,请使用正版授权;侵权联删。推荐最新 IntelliJ I
- mybatis自动生成代码(实体类、Dao接口等)是很成熟的了,就是使用mybatis-generator插件。 它是一个开源的插件,使用m
- 目录SpringBoot整合OpenApiOpenAPI依赖编写配置类改造优化OpenAPI常用注解介绍实体类controller类演示网上
- 1.栈和队列的共同特点是(只允许在端点处插入和删除元素)4.栈通常采用的两种存储结构是(线性存储结构和链表存储结构)5.下列关于栈的叙述正确
- 本文实例讲述了Java二维数组简单定义与使用方法。分享给大家供大家参考,具体如下:Java的二维数组是先创建一个一维数组,然后该数组的元素再
- DllImport是System.Runtime.InteropServices命名空间下的一个属性类,其功能是提供从非托管DLL导出的函数
- 最近做了很多项目,不同的系统,不同的部署方式,这里做个记录1.在jar包目录新建一个start.bat 文件,然后写入启动命令j
- springcloud-gateway集成knife4j环境信息环境信息spring-boot:2.6.3spring-cloud-alib
- @ModelAttribute与@RequestBody的区别最近在写代码的过程中,发现之前项目都是使用的@ModelAttribute注解
- 1. 概述锁是Java并发编程中最重要的同步机制。锁除了让临界区互斥执行外,还可以让释放锁的线程获取同一个锁的线程发送消息。锁在实际使用时只
- 前言嵌套查询的实现原理为两次查询,比如产品表为主表,图片表为从表通过product_id字段与产品表id字段关联实现一对多,嵌套查询 首先查
- 前言Zuul 是Netflix 提供的一个开源组件,致力于在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。也有很多公司使用它来作为
- java 读取网页内容的实例详解import java.io.BufferedReader; import java.io.IOExcept
- 在上面的例子中多次使用到了Thread类的join方法。我想大家可能已经猜出来join方法的功能是什么了。对,join方法的功能就是使异步执
- 堆区:只存放类对象,线程共享;方法区:又叫静态存储区,存放class文件和静态数据,线程共享;栈区:存放方法局部变量,基本类型变量区、执行环
- 由于要在Web项目中采用RFID读取功能,所以有必要开发Activex,一般情况下开发Activex都采用VC,VB等,但对这两块不是很熟悉
- properties和yml的区别这几天刚好看到Spring Boot当中有两种配置文件的方式,但是这两种配置方式有什么区别呢?proper
- 一、前言Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用