• Fegin参数使用总结


    注解:

    @RequestParam:前端传值时要在URL后面拼接 参数名+参数值
    用法:@RequestParam(value="nnn",required=false),Post和Get都可以使用
    @RequestBody:从请求体中获取数据
    用法:支持Post
    @PathVariable:前端传值时,不需要参数名,直接在URL后面拼接参数值即可,url/typeId,用来获取url中的动态参数
    用法:只支持Get
    get和post请求中对于传递单个引用类型的参数,比如String,Integer....用@RequestParam(),括号中一定要有值(参数的别名)。调用方需要加注解,被调用方不需要加。当然加上也不会出错。被调用方的参数名和调用方的别名保持一致即可。
     
    post请求中对于javaBean,map,list类型的参数的传递,用@RequestBody,调用方不需要加注解,被调用方加注解即可。 注:get请求中使用@RequestBody会出错,同时也不能传递javaBean,map,list类型的参数。
    参考文章:http://www.javashuo.com/article/p-parfdzuq-mg.html

    (1)无参请求

    Post和Get没有区别,均只需要修改RequestMethod请求类型就可以
    /**
     * 调用过程代码
     */
    public Result<?> queryParticipantsList(){
       System.out.println(iSysBaseAPI.feignTestDemo());
       return new Result<>().ok(null);
    }
    
    /** 
      * 在Feign的Api接口和实现类的代码如下
      * @Description Feign调用测试 get和post无参调用
      */
    @RequestMapping(value = "/sys/api/feignTestDemo",method = RequestMethod.GET)
    public String feignTestDemo();
    @RequestMapping(value = "/feignTestDemo",method = RequestMethod.GET)
    public String feignTestDemo(){
        return "Get调用测试";
    }
    
    @RequestMapping(value = "/sys/api/feignTestDemo",method = RequestMethod.POST)
    public String feignTestDemo();
    @RequestMapping(value = "/feignTestDemo",method = RequestMethod.POST)
    public String feignTestDemo(){
        return "Post无参调用测试";
    }
    
    @RequestMapping(value = "/sys/api/feignTestDemo",method = RequestMethod.POST)
    public List<SysUser> feignTestDemo();
    @RequestMapping(value = "/feignTestDemo",method = RequestMethod.POST)
    public List<SysUser> feignTestDemo(){
        List<SysUser> list=new ArrayList<>();
        SysUser sysUser=new SysUser();
        sysUser.setId("110");
        sysUser.setUsername("Post无参调用测试");
        list.add(sysUser);
        return list;
    }
    
    @RequestMapping(value = "/sys/api/feignTestDemo",method = RequestMethod.GET)
    public List<SysUser> feignTestDemo();
    @RequestMapping(value = "/feignTestDemo",method = RequestMethod.GET)
    public List<SysUser> feignTestDemo(){
        List<SysUser> list=new ArrayList<>();
        SysUser sysUser=new SysUser();
        sysUser.setId("110");
        sysUser.setUsername("Post无参调用测试");
        list.add(sysUser);
        return list;
    }

    (2)有参请求

    get请求

    第一种Get请求,没有使用任何注解,结果会提示错误
    public Result<?> queryParticipantsList(){
       String title="String参数,Get请求,没有使用注解";
       System.out.println(iSysBaseAPI.feignTestDemo(title));
       return new Result<>().ok(null);
    }
    @RequestMapping(value = "/sys/api/feignTestDemo",method = RequestMethod.GET)
    public String feignTestDemo(String title);
    @RequestMapping(value = "/feignTestDemo",method = RequestMethod.GET)
    public String feignTestDemo(String title){
        return title;
    }
    错误信息如下:
    org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported
    
    第二种情况 使用@RequestParam("title"),正常没有任何问题
    public Result<?> queryParticipantsList(){
       String title="String参数,Get请求,使用@RequestParam(\"title\")";
       System.out.println(iSysBaseAPI.feignTestDemo(title));
       return new Result<>().ok(null);
    }
    @RequestMapping(value = "/sys/api/feignTestDemo",method = RequestMethod.GET)
    public String feignTestDemo(@RequestParam("title") String title);
     @RequestMapping(value = "/feignTestDemo",method = RequestMethod.GET)
    public String feignTestDemo(@RequestParam("title")String title){
        return title;
    }
    
    第三种情况:当传递的参数是List<String>和List<T>的时候,当时List<String>正常调用,没有任何问题
    public Result<?> queryParticipantsList(){
       String title="String参数,Post请求,使用@RequestParam(\"title\")";
       List<String> titles=new ArrayList<>();
       titles.add(title);
       System.out.println(iSysBaseAPI.feignTestDemo(titles));
       return new Result<>().ok(null);
    }
    @RequestMapping(value = "/sys/api/feignTestDemo",method = RequestMethod.GET)
    public List<String> feignTestDemo(@RequestParam(name = "titles") List<String> titles);
    @RequestMapping(value = "/feignTestDemo",method = RequestMethod.GET)
    public List<String> feignTestDemo(@RequestParam(name = "titles")List<String> titles){
        return titles;
    }
    当传递的是List<实体的时候,会出现错误>,错误信息如下
    public Result<?> queryParticipantsList(){
       SysUser sysUser=new SysUser();
       sysUser.setUsername("get请求,传送List<SysUser>参数");
       sysUser.setId("110");
       ArrayList<SysUser> titles=new ArrayList<>();
       titles.add(sysUser);
    
       System.out.println(iSysBaseAPI.feignTestDemo(titles));
       return new Result<>().ok(null);
    }
    @RequestMapping(value = "/sys/api/feignTestDemo",method = RequestMethod.GET)
    public List<SysUser> feignTestDemo(@RequestParam(name = "titles") ArrayList<SysUser> titles);
    2021-12-16 15:56:54.372 [http-nio-7001-exec-7] ERROR o.jeecg.common.exception.JeecgBootExceptionHandler:69 - Failed to convert value of type 'java.lang.String' to required type 'java.util.ArrayList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.ArrayList': no matching editors or conversion strategy found
    @RequestMapping(value = "/feignTestDemo",method = RequestMethod.GET)
    public List<SysUser> feignTestDemo(@RequestParam(name = "titles")ArrayList<SysUser> titles){
        return titles;
    }
    
    第四种情况 使用@PathVariable("title"),错误
    错误原因:无法在url中拼接参数,后期需要询问讨论一下。
    public Result<?> queryParticipantsList(){
       String title="String参数,Get请求,使用@PathVariable(\"title\")";
       System.out.println(iSysBaseAPI.feignTestDemo(title));
       return new Result<>().ok(null);
    }
    @RequestMapping(value = "/sys/api/feignTestDemo/{title)}",method = RequestMethod.GET)
    public String feignTestDemo(@PathVariable(value = "title") String title);
    @RequestMapping(value = "/feignTestDemo/{title)}",method = RequestMethod.GET)
    public String feignTestDemo(@PathVariable(value = "title") String title){
        return title;
    }
    错误信息如下:
    org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported

    post请求

    第一种,单参数,使用@RequestParam传递,没问题
    public Result<?> queryParticipantsList(){
       String title="String参数,Post请求,使用@RequestParam(\"title\")";
       System.out.println(iSysBaseAPI.feignTestDemo(title));
       return new Result<>().ok(null);
    }
    @RequestMapping(value = "/sys/api/feignTestDemo",method = RequestMethod.POST)
    public String feignTestDemo(@RequestParam(name = "title") String title);
    @RequestMapping(value = "/feignTestDemo",method = RequestMethod.POST)
    public String feignTestDemo(@RequestParam(name = "title") String title){
        return title;
    }
    
    第二种传递实体的时候,使用@RequestParam和@RequestBody两种方式进行测试
    采用@RequestParam注解的,失败
    public Result<?> queryParticipantsList(){
       SysUser sysUser=new SysUser();
       sysUser.setUsername("Post请求,传送List<SysUser>参数");
       sysUser.setId("110");
       ArrayList<SysUser> titles=new ArrayList<>();
       titles.add(sysUser);
    
       System.out.println(iSysBaseAPI.feignTestDemo(titles));
       return new Result<>().ok(null);
    }
    @RequestMapping(value = "/sys/api/feignTestDemo",method = RequestMethod.POST)
    public List<SysUser> feignTestDemo(@RequestParam(name = "titles")ArrayList<SysUser> titles);
    @RequestMapping(value = "/feignTestDemo",method = RequestMethod.POST)
    public List<SysUser> feignTestDemo(@RequestParam(name = "titles")ArrayList<SysUser> titles){
        return titles;
    }
    错误信息如下:
    2021-12-16 16:11:58.725 [http-nio-7001-exec-7] ERROR o.jeecg.common.exception.JeecgBootExceptionHandler:69 - Failed to convert value of type 'java.lang.String' to required type 'java.util.ArrayList'; nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.util.ArrayList': no matching editors or conversion strategy found
    
    采用@RequestBody注解的,成功
    public Result<?> queryParticipantsList(){
       SysUser sysUser=new SysUser();
       sysUser.setUsername("Post请求,传送List<SysUser>参数");
       sysUser.setId("110");
       ArrayList<SysUser> titles=new ArrayList<>();
       titles.add(sysUser);
    
       System.out.println(iSysBaseAPI.feignTestDemo(titles));
       return new Result<>().ok(null);
    }
    @RequestMapping(value = "/sys/api/feignTestDemo",method = RequestMethod.POST)
    public List<SysUser> feignTestDemo(@RequestBody ArrayList<SysUser> titles);
    @RequestMapping(value = "/feignTestDemo",method = RequestMethod.POST)
    public List<SysUser> feignTestDemo(@RequestBody ArrayList<SysUser> titles){
        return titles;
    }
    第三种传递多种参数,使用@RequestParam和@RequestBody进行混合使用,没问题
    public Result<?> queryParticipantsList(){
       SysUser sysUser=new SysUser();
       sysUser.setUsername("Post请求,传送List<SysUser>参数");
       sysUser.setId("110");
       ArrayList<SysUser> titles=new ArrayList<>();
       titles.add(sysUser);
    
       String name="Post,多参数测试";
    
       System.out.println(iSysBaseAPI.feignTestDemo(titles,name));
       return new Result<>().ok(null);
    }
    @RequestMapping(value = "/sys/api/feignTestDemo",method = RequestMethod.POST)
    public List<SysUser> feignTestDemo(@RequestBody ArrayList<SysUser> titles,@RequestParam(name = "name")String name);
    @RequestMapping(value = "/feignTestDemo",method = RequestMethod.POST)
    public List<SysUser> feignTestDemo(@RequestBody ArrayList<SysUser> titles,@RequestParam(name = "name") String name){
        return titles;
    }
  • 相关阅读:
    vs2010启动调试很慢
    vs2010 cannot file the PDB file解决
    hadoop的版本到底什么样子
    关于spring 或者qurtz的配置
    eclipse 关于内嵌maven
    spring学习 mark一下
    spring mvc视图解析
    rcp项目
    邮件发送
    ASP.NET 缓存学习
  • 原文地址:https://www.cnblogs.com/qcq0703/p/15699126.html
Copyright © 2020-2023  润新知