目錄
? ? ??feign實(shí)現(xiàn)http遠(yuǎn)程調(diào)用(正文)
??????解決方案:
在正式講解HTTP遠(yuǎn)程調(diào)用時(shí),我們先來了解一下缺省方法的定義。
寫在前面:
- 缺省參數(shù)可傳可不傳,可以傳多個(gè)但他們必須是相同的類型
- 每個(gè)方法中缺省參數(shù)只能定義一個(gè),并且只能在參數(shù)的最后定義;
- 缺省參數(shù)可讀性比較差,而且它還可以被數(shù)組代替,開發(fā)中不建議使用,這里遇到了就簡單來了解一下;
????????缺省方法:定義N個(gè)相同類型的參數(shù)值,N >= 0;命名方式就是在參數(shù)前增加三個(gè)點(diǎn)省略符,如下例子可以幫我們更好的理解這一概念:
public class Student{
public static void getStudent(int age, String name, String ...strB ){
...
}
}
? ? ? ? 我們可以采用如下方式對缺省方法進(jìn)行調(diào)用:
public static void main(String[] args) {
Student.getStudent(22,"Jimmy"); // 沒有
Student.getStudent(22,"Jimmy","strB1"); // 1個(gè)
Student.getStudent(22,"Jimmy","strB1","strB2"); // 多個(gè)
}
? ? ? ? 缺省參數(shù)值的遍歷(foreach),比較簡單就不過多闡述,直接看一下例子:
public class Student{
public static void getStudent(int age, String name, String ...strB ){
System.out.println("參數(shù)數(shù)量:" + strB.length);
for (String str : strB) {
System.out.println("參數(shù)值:" + str);
}
}
}
? ? ??feign實(shí)現(xiàn)http遠(yuǎn)程調(diào)用(正文)
????????首先我們需要引入所需要的依賴,這里值得注意的是,我們在引入依賴時(shí)所選用的springboot版本必須和springcloud保持對應(yīng),這里我是用的是springboot【2.2.6.RELEASE】+springcloud【Hoxton.SR12】進(jìn)行演示操作。
<dependencyManagement>
<dependencies>
<!--SpringCloud依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR12</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--openfeign跨服務(wù)調(diào)用-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--openfeign底層使用ApacheHttpClient調(diào)用-->
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
</dependencies>
????????Feign支持通過GET和POST方法向服務(wù)傳遞參數(shù),我們先來簡單看一個(gè)例子:
????????這里@FeignClient中的name代表的是微服務(wù)名稱標(biāo)識,path是Spring Boot配置環(huán)境中的server.servlet.context-path=/test,用來標(biāo)明是哪個(gè)應(yīng)用下的微服務(wù)。
? ? ? ? 當(dāng)然這里也可以直接通過value來定義http://xx.xx.xxx.xx:xxxx來進(jìn)行遠(yuǎn)程調(diào)用。
@FeignClient(name = "service", path = "/test")
public interface Service {
@GetMapping(value = "/get")
TestMode echoGet(@RequestParam("parameter") String parameter);
@PostMapping(value = "/post")
TestMode echoPost(@RequestParam("parameter") String parameter);
}
? ? ? ? 頭疼的是我在采用上述@RequestParam("parameter")方式的時(shí)候,出現(xiàn)了下面的報(bào)錯(cuò):
GET "/test/post?parameter=POST%20%3A%20++%20plus%20sign%20contained%2C%20but%20it's%20gone%20unexpectedly."
????????剛開始我對這個(gè)報(bào)錯(cuò)比較迷惑,經(jīng)過查閱資料發(fā)現(xiàn)這種URL傳參的方式存在一定的風(fēng)險(xiǎn),因?yàn)樗?strong>URL長度是有限制的,如果我們傳遞的數(shù)據(jù)量很大,這時(shí)候就會出現(xiàn)上述錯(cuò)誤。
??????解決方案:
????????針對上述情況,我通過feign客戶端以form表單形式提交(POST
)參數(shù)的方式進(jìn)行解決。
????????將某個(gè)feign
客戶端方法定義如下 :
@Component
@FeignClient(value = "http://xx.xx.xxx.xx:xxxx")
public interface TestPaperClient {
@RequestMapping(value = "/xxx",method = RequestMethod.POST,consumes = {"application/x-www-form-urlencoded"})
TestPaper paperGeneration(Map<String, ?> map);
}
注意:
- 使用注解 @RequestMapping 表明要使用POST方法,當(dāng)然這里也可以直接使用@PostMapping更加方便。
- 注解中要添加屬性consumes = {"application/x-www-form-urlencoded"}。
- 參數(shù)使用Map<String, ?>形式 (不再是@RequestParam("parameter"))。
- Map<String, ?>中的
key
要和遠(yuǎn)程服務(wù)器端要求的參數(shù)名稱一致,value
的類型要和遠(yuǎn)程服務(wù)器端要求的類型一致(可以借助下面serviceImpl中的代碼進(jìn)行理解),否則會出現(xiàn)HTTP 400 BAD_REQUEST
錯(cuò)誤。
如下代碼可以輔助理解上面的文字說明:
? ? ? ? 注意:下面例子中有一個(gè)自己封裝的ApiResponse類,用來返回狀態(tài)碼和相關(guān)信息,大家可以根據(jù)自己的需求進(jìn)行封裝。
遠(yuǎn)程服務(wù)器返回的內(nèi)容封裝為實(shí)體類:
@Component
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TestPaper {
private String text;//服務(wù)器端要返回的內(nèi)容
}
Controller層代碼:
@RestController
@RequestMapping(value = "/feign")
public class TestPaperController {
@Autowired
TestPaperService testPaperService;
@PostMapping("/DKQA")
public ApiResponse<TestPaper> testPaper(@RequestParam("msg") String msg){
return testPaperService.testPaper(msg);
}
}
Service接口代碼:
public interface TestPaperService {
ApiResponse<TestPaper> testPaper(String testPaper);
}
service接口實(shí)現(xiàn)類代碼:文章來源:http://www.zghlxwxcb.cn/news/detail-401742.html
@Service
public class TestPaperServiceImpl implements TestPaperService {
@Autowired
private TestPaperClient testPaperClient;
@Override
public ApiResponse<TestPaper> testPaper(String testPaper) {
Map<String,Object> map = new HashMap<>();
map.put("msg",testPaper);
return ApiResponse.data(testPaperClient.paperGeneration(map));
}
}
????????基于這種方式傳遞參數(shù),我們就不用太擔(dān)心參數(shù)數(shù)據(jù)量過大。至此,遇到的問題已經(jīng)解決,后續(xù)有相關(guān)問題會再加以補(bǔ)充。文章來源地址http://www.zghlxwxcb.cn/news/detail-401742.html
到了這里,關(guān)于Http遠(yuǎn)程調(diào)用(feign客戶端通過POST傳遞FORM格式數(shù)據(jù))的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!