SpringCloud Feign參數問題及解決方法

這篇文章主要介紹了SpringCloud Feign參數問題及解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

今天遇到使用Feign調用微服務,傳遞參數時遇到幾個問題

1.無參數

以GET方式請求

服務提供者

1

2

3

4

@RequestMapping("/hello")

public String Hello(){

  return "hello,provider";

}

服務消費者

1

2

@GetMapping("/hello")

String hello();

2.單個參數

(1)GET——@PathVariable

服務提供者

1

2

3

4

@GetMapping("/test/{name}")

public String test(@PathVariable String name){

  return "hello,"+name;

}

服務消費者

1

2

@GetMapping("/test/{name}")

String test(@PathVariable("name") String name);

(2)GET——@RequestParam

服務提供者

1

2

3

@RequestMapping("/test")

public String test(String name){return "hello,"+name;

}

服務消費者

1

2

@RequestMapping("/test")

String test(@RequestParam String name);

會遇到報錯

1

RequestParam.value() was empty on parameter 0

解決方法:

  加上註解的描述,修改爲

1

2

@RequestMapping("/test")

String test(@RequestParam("name") String name);

(3)POST

@RequestBody

不需要註解的描述

1

2

@RequestMapping("/test")

String test(@RequestBody String name);

注:

  •   參數前使用了@RequestBody註解的,都以POST方式消費服務
  •   @RequestBody註解的參數,需要POST方式才能傳遞數據

2.Feign多參數的問題

(1)GET——@PathVariable

服務提供者

1

2

3

4

@GetMapping("/test/{name}/{xyz}")

public String test(@PathVariable String name,@PathVariable String xyz){

    return "hello,"+name+","+xyz;

}<br>

服務消費者

1

2

@GetMapping("/test/{name}/{xyz}")

String test(@PathVariable("name") String name,@PathVariable("xyz") String xyz);

(1)GET——@RequestParam

服務提供者

1

2

3

4

5

6

7

8

@RequestMapping("/test")

public String test(String name,Integer type){

  if(type==1){

    return "hello,"+name;

  }else{

    return "hello,provider-"+name;

  }

}

服務消費者

1

2

@RequestMapping("/test")

String test(String name, Integer type);

會遇到報錯Method has too many Body parameters

說明:

  如果服務消費者傳過來參數時,全都用的是@RequestParam的話,那麼服務提供者的Controller中對應參數前可以寫@RequestParam,也可以不寫

  服務消費者feign調用時,在所有參數前加上@RequestParam註解

正確的寫法

1

2

@RequestMapping("/test")

String test(@RequestParam("name") String name, @RequestParam("type") Integer type);

(2)POST

如果接收方不變

服務消費者

1

2

@RequestMapping("/test")

String test(@RequestBody String name, @RequestBody Integer type);

會遇到報錯Method has too many Body parameters

服務消費者爲

1

2

@RequestMapping("/test")

String test(@RequestBody String name, @RequestParam("type") Integer type);

name的值會爲null

說明:

如果服務消費者傳過來參數,有@RequestBody的話,那麼服務提供者的Controller中對應參數前必須要寫@RequestBody

正確的寫法

服務提供者

1

2

3

4

5

6

7

8

@RequestMapping("/test")

  public String test(@RequestBody String name, Integer type){

    if(type==1){

      return "hello,"+name;

    }else{

      return "hello,provider-"+name;

    }

  }

服務消費者正確的寫法

1

2

@RequestMapping("/test")

String test(@RequestBody String name, @RequestParam("type") Integer type);

可以接收到參數

總結:

  •   請求參數前加上註解@PathVariable、@RequestParam或@RequestBody修飾
  •   可以有多個@RequestParam,但只能有不超過一個@RequestBody
  •   使用@RequestParam註解時必須要在後面加上參數名
  •   @RequestBody用來修飾對象,但是既有@RequestBody也有@RequestParam,那麼參數就要放在請求的url中,@RequestBody修飾的就要放在提交對象中
  •   當參數比較複雜時,feign即使聲明爲get請求也會強行使用post請求
  •  
  • https://www.jb51.net/article/176075.htm
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章