首页 > 其他分享 >fegin参数传递

fegin参数传递

时间:2023-09-28 11:00:12浏览次数:24  
标签:fegin RequestParam admin selectOne 参数传递 result id RequestMapping

Feign传递参数
传递单个参数:
单个参数的传值有两种方式,第一种
使用@RequestParam/@PathVariable进行传值

客户端feign调用接口(@RequestParam)

@RequestMapping("/ct/selectOne")
Customer selectOne(@RequestParam("id") Integer id);

 

服务提供端

@RequestMapping("selectOne")
public Customer selectOne(Integer id) {
return this.customerService.queryById(id);


客户端feign调用接口(@PathVariable)

@GetMapping("/admin/selectOne/{id}")
String selectOne(@PathVariable("id") Integer id);


服务提供端

@RequestMapping("selectOne/{id}")
@HystrixCommand(fallbackMethod = "HystrixqueryById")
public Admin selectOne(@PathVariable("id") Integer id) {
Admin bean = adminService.queryById(id);
if(bean == null){
throw new RuntimeException("id:"+id+"没有找到该id的用户");
}
return bean;
}


注意:
1、在使用@RequestParam/@PathVariable进行传值时,一定要注意,需要绑定参数,如@RequestParam(“id”)绑定id,不然会报错

2、@PathVariable是获取url上数据的,@RequestParam获取请求参数的(包括post表单提交)

传递多个参数:
多个参数的传值可以使用多个@RequestParam来进行传参

客户端feign调用接口

@RequestMapping("/ct/upload")
Customer upload(@RequestParam("newFileName") String newFileName,
@RequestParam("id") int id);

 

服务提供端

@RequestMapping("upload")
public Customer upload(String newFileName,int id) throws IOException {

 

System.out.println("进入提供者-图片上传");
//设置图片上传路径,是目标文件夹的路径

// 保存到数据库

Customer customer=customerService.queryById(id);
customer.setImage(newFileName);

customerService.update(customer);

return customer;

}


传对象:
传对象有两种方式

第一种,使用@SpringQueryMap注解实现

客户端feign调用接口

@RequestMapping("/ev/insert")
Evaluation insert(@SpringQueryMap Evaluation evaluation);


服务提供端

@RequestMapping("insert")
public Evaluation insert(Evaluation evaluation) {
return evaluationService.insert(evaluation);
}


第二种,使用@RequestBody传递参数

客户端feign调用接口

@RequestMapping(value = "/admin/save", method = RequestMethod.POST)
Object save(@RequestBody Admin admin);


服务提供端

@PostMapping("save")
public Object save(@RequestBody Admin admin){
boolean result = false;
//判断是添加还是编辑
if(admin.getId()!=null){


//编辑

// System.out.println("编辑管理员信息");
result = adminService.update(admin)>0;
} else {
//添加
admin.setRegDate(new Date());
// System.out.println("添加管理员信息"+admin);
result = adminService.insert(admin).getId() != null;
}
return result;
}


重点:多个参数+对象的传值
在进行多个参数+对象传值时,使用@RequestParam来传递普通参数,使用@SpringQueryMap来传递对象

注:
本人亲测踩坑
使用@RequestParam+@RequestBody的时候,出现问题
@RequestBody要求前端页面返回json格式,否则会报:不支持Content-Type:application/json的错误

客户端feign调用接口

@RequestMapping(value = "/admin/queryAll", method = RequestMethod.POST)
String queryAll(@RequestParam("page") Integer page,
@RequestParam("limit") Integer limit,
@SpringQueryMap AdminQuery admin);


服务提供端

@PostMapping("queryAll")
public Object queryAll(Integer page, Integer limit,AdminQuery admin) {
CommonResult<Admin> result = new CommonResult<>();
IPage<Admin> ipage = adminService.queryAllByLimit(page,limit,admin);
result.setCode(0);
result.setCount(ipage.getTotal());
result.setData(ipage.getRecords());
return result;

}

 

标签:fegin,RequestParam,admin,selectOne,参数传递,result,id,RequestMapping
From: https://www.cnblogs.com/ckfeng/p/17735214.html

相关文章

  • sv 变量赋值,参数传递
    systemverilog变量赋值,参数传递1、变量类型systemverilog中的变量可以分为两种,一种普通变量类型,一种是句柄变量类型。普遍变量跟C/C++中的普通变量一样,而句柄变量则与C/C++中的指针变量或者引用变量类似。内置类型,比如int,bit,这些类型定义的变量都是普通变量。自定义的cla......
  • Postman设置全局变量、参数传递、断言
    搬砖地址:https://blog.csdn.net/lzz718719/article/details/129671324 ......
  • angular event服务,不同组件间参数传递
    利用AngularEvent在不同组件之间传递数据为了实现在Angular不同Component之间相互传递数据,可以使用Event分发的思路来实现。使用事件实现在不同组件之前传递数据的思路如下:定义一个服务,用来实现事件的发布和订阅方法。组件A注入事件服务的依赖,将自己要传递数据的数据以事件的形式......
  • 参数传递和返回值
    一、passbyvaluevs.passbyreference(toconst) passbyvalue:这个数据有多大,就整个包传递,压入到栈中。尽量不要这样传。passbyreference:像指针但是更漂亮。引用在底部就是个指针,尽量都传引用。例子中传引用且加了const,不希望传过去被更改。二、returnbyvaluevs.r......
  • struts2中的参数传递
    这个问题其实一直很困惑我的,在写平常的jsp程序时,传递参数很容易,通过表单,request,链接等都可以传递,但是到了struts2中,在写的各个地方,都看不到任何的request或是response,不知道该怎么传递参数了,到了今天学习了struts2中的参数传递这一节,终于解开了疑惑,但是还不是很清楚,有待以后探索。......
  • GO语言中的参数传递
    在Go语言中,函数参数传递有两种方式:值传递和引用传递。值传递(PassbyValue):当将一个值作为参数传递给函数时,函数会创建该值的一个副本,并将副本传递给函数。在函数内部,对参数的修改不会影响原始值。下面是一个示例:packagemainimport"fmt"funcmodifyValue(xint){x......
  • 实现Fegin最佳实践
              ......
  • Fegin的最佳实践
          ......
  • pytest 多参数传递时的坑 参数数组格式 :[(参数1,参数2)(参数1,参数2)] ;一个参数传递时
    一个参数传递时结果:(参数1) 两个参数传递时:参数1参数2 ......
  • 在Python中,当你调用一个类的方法时,需要将类的实例作为第一个参数传递给方法。 括号
    classClass_test:def__init__(self):pass#若无则报错AttributeError:'Class_test'objecthasnoattribute'fun_zip'deffun_zip(self,df_arg=pd.DataFrame(),bool_arg=False):#......