1.引入所需依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.调用接口
package example.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;
@RestController
@Slf4j
public class TestController {
@RequestMapping("/api/rpcTest")
public Map<String, Object> testFeignForm(@RequestParam Map<String,Object> params,@RequestParam("file") MultipartFile file) {
System.out.println(params);
System.out.println(file.getOriginalFilename());
return new HashMap();
}
}
3.Feign配置
package com.example.rpc;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import java.io.File;
import java.util.Map;
@FeignClient(name = "test", url = "http://localhost:8081")
public interface TestFeign {
@PostMapping(value = "/api/rpcTest", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
Map<String, Object> rpcTest(@RequestParam Map params, @RequestPart File file);
}
调用结果