文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

SpringCloudFeign多参数传递及需要注意的问题

2024-04-02 19:55

关注

Feign多参数传递及注意的问题

这边沿用前面的Eureka,Feign,Service

在服务提供者cloud-shop-userservice中新增几个方法


	@PostMapping("/user")
	public String aveUser(@RequestBody User user) {
		logger.info("保存用户 :" +user.toString());
		return "Success";
	}
	
	
	@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中调用方法

	
	@PostMapping("/user")
	public String aveUser(@RequestBody User user) {
		return userService.aveUser(user);
	}
	
	
	@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;

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;

@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;

@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);
    }
    
    @PostMapping("/expire")
    public Boolean expire(String key, Long time) {
        return redisUtil.expire(key, time);
    }
    
    @PostMapping("/getExpire")
    public Long getExpire(String key) {
        return redisUtil.getExpire(key);
    }
    
    @PostMapping("/hasKey")
    public Boolean hasKey(String key) {
        return redisUtil.hasKey(key);
    }
    
    @SuppressWarnings("unchecked")
    @PostMapping("/del")
    public void del(@RequestParam String[] keys) {
        redisUtil.del(keys);
    }
    //===============================String===============================
    
    @PostMapping("/string/get")
    public String getString(String key) {
        return redisString.get(key).toString();
    }
    
    @PostMapping("/string/set")
    public Boolean setString(String key, String value) {
        return redisString.set(key, value);
    }
    
    @PostMapping("/string/setValueAndTime")
    public Boolean setValueAndTime(String key, String value, Long time) {
        return redisString.set(key, value, time);
    }
    
    @PostMapping("/string/incr")
    public Long incrString(String key, Long delta) {
        return redisString.incr(key, delta);
    }
    
    @PostMapping("/string/decr")
    public Long decrString(String key, Long delta) {
        return redisString.decr(key, delta);
    }
    //===============================Map===============================
    
    @PostMapping("/map/get")
    public String getMap(String key, String item) {
        return redisMap.get(key, item);
    }
    
    @PostMapping("/map/getAll")
    public Map<Object, Object> getAllMap(String key) {
        return redisMap.getAll(key);
    }
    
    @PostMapping("/map/set")
    public Boolean setMap(String key, @RequestParam Map<String, Object> map) {
        return redisMap.set(key, map);
    }
    
    @PostMapping("/map/setMapAndTime")
    public Boolean setMapAndTime(@RequestParam String key,@RequestParam Map<String, Object> map,@RequestParam Long time) {
        return redisMap.set(key, map, time);
    }
    
    @PostMapping("/map/setMapItem")
    public Boolean setMapItem(String key, String item, String value) {
        return redisMap.set(key, item, value);
    }
    
    @PostMapping("/map/setMapItemAndTime")
    public Boolean setMapItemAndTime(String key, String item, String value, Long time) {
        return redisMap.set(key, item,value, time);
    }
    
    @PostMapping("/map/del")
    public void delMap(String key,@RequestParam Object[] items) {
        redisMap.del(key, items);
    }
    
    @PostMapping("/map/hashKey")
    public Boolean mhashKey(String key, String item) {
        return redisMap.hasKey(key, item);
    }
    
    @PostMapping("/map/incr")
    public Double incrMap(String key, String item, Double delta) {
        return redisMap.incr(key, item, delta);
    }
    
    @PostMapping("/map/decr")
    public Double decrMap(String key, String item, Double delta) {
        return redisMap.decr(key, item, delta);
    }
    //===============================Set===============================
    
    @PostMapping("/set/get")
    public Set<Object> getSet(String key) {
        return redisSet.get(key);
    }
    
    @PostMapping("/set/setValue")
    public Long setValue(String key,@RequestParam Object[] values) {
        return redisSet.set(key, values);
    }
    
    @PostMapping("/set/hashKey")
    public Boolean hashKey(String key, String value) {
        return redisSet.hasKey(key, value);
    }
    
    @PostMapping("/set/setValueAndTime")
    public Long setValueAndTime(String key, Long time,@RequestParam Object[] values) {
        return redisSet.set(key, time, values);
    }
    
    @PostMapping("/set/getSize")
    public Long getSize(String key) {
        return redisSet.getSize(key);
    }
    
    @PostMapping("/set/remove")
    public Long remove(String key,@RequestParam Object[] values) {
        return redisSet.remove(key, values);
    }
    //===============================List===============================
    
    @PostMapping("/list/get")
    public List<Object> get(String key, Long start, Long end) {
        return redisList.get(key, start, end);
    }
    
    @PostMapping("/list/getSize")
    public Long getListSize(String key) {
        return redisList.getSize(key);
    }
    
    @PostMapping("/list/getByIndex")
    public Object getByIndex(@RequestParam("key") String key, @RequestParam("index") Long index) {
        return redisList.getByIndex(key, index);
    }
    
    @PostMapping("/list/setValue")
    public Boolean setValue(String key, Object value) {
        return redisList.set(key, value);
    }
    
    @PostMapping("/list/setValueAndTime")
    public Boolean setValueAndTime(String key, Object value, Long time) {
        return redisList.set(key, value, time);
    }
    
    @PostMapping("/list/setList")
    public Boolean setList(String key, List<Object> value) {
        return redisList.set(key, value);
    }
    
    @PostMapping("/list/setListAndTime")
    public Boolean setListAndTime(String key, List<Object> value, Long time) {
        return redisList.set(key, value, time);
    }
    
    @PostMapping("/list/updateByIndex")
    public Boolean updateByIndex(String key, Long index, Object value) {
        return redisList.updateIndex(key, index, value);
    }
    
    @PostMapping("/list/remove")
    public Long remove(String key, Long count, Object value) {
        return redisList.remove(key, count, value);
    }
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯