通常我们学习OpenFeign的时候,只是做最简单的处理,post/get请求传递简单的参数,但是如果我们上传的是一个文件的时候应该怎么办?
一. OpenFeign文件上传
我们首先需要了解几个知识点,用来为后面文件的上传做准备。
ContentType和Accept
- ContentType
ContentType指的是服务器当前发送的数据是什么数据
- Accept
Accept指的是用来告诉服务器客户端接收的是什么类型的数据,只有发送该类型的数据才可以识别
@RequestMapping中的produces和consumes
- consumes
用来限制Http请求的ContentType
- produces
produces用来限制Accept
举个简单的例子来讲述:
@RequestMapping(value = "/dealFile", method = RequestMethod.POST,
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
上面的代码的意思是,用户发送的数据类型是表单数据,但是接收只能是JSON格式的数据。
二. 代码详解
这里我们的代码架构大致如下图所示:
我们有一个根节点,下面是各自的Module,我们将其通用的maven依赖都放在该test-openfeign下
test-openfeign
- pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<packaging>pom</packaging>
<modules>
<module>test-feign</module>
<module>test-eureka-server</module>
<module>test-file-deal</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.xiyou</groupId>
<artifactId>test-openfeign</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>test-openfeign</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Eureka代码
- pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>test-openfeign</artifactId>
<groupId>com.xiyou</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-eureka-server</artifactId>
</project>
- application.yml文件
server:
port: 8761
spring:
application:
name: test-eureka-server
eureka:
instance:
hostname: localhost
instance-id: eureka-instance
client:
fetch-registry: false
register-with-eureka: false
service-url:
default-zone: http://${eureka.instance.hostname}:${server.port}/eureka/
该yml主要规定了eureka的服务端相关配置
- 主启动类 EurekaApplication
package com.xiyou.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
文件处理模块(test-file-deal)
该模块是消费者,即OpenFeign的接收端,接受从别的服务发送来的文件的对象
- pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>test-openfeign</artifactId>
<groupId>com.xiyou</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-file-deal</artifactId>
</project>
- application.yml
server:
port: 8762
spring:
application:
name: test-file-deal
eureka:
instance:
instance-id: file-deal
client:
service-url:
default-zone: http://localhost:8761/eureka/
- 启动类FileApplication
package com.xiyou.filedeal;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableEurekaClient
public class FileApplication {
public static void main(String[] args) {
SpringApplication.run(FileApplication.class, args);
}
}
- 文件处理FileDeal
这里只是对其简单的打印操作,因为我们关注的重点是文件参数能否通过feign传递
package com.xiyou.filedeal.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class FileDeal {
@PostMapping("/dealFile")
public String fileDeal(@RequestBody MultipartFile file){
System.out.println(file.getName());
System.out.println(file.getOriginalFilename());
return file.getName();
}
}
Feign的发送端(test-feign)
- 前端发送文件的form
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<form action="/upload" id= "uploadForm" enctype="multipart/form-data" method="post">
<p >指定文件名: <input type="text" name="filename" value= ""/></p >
<p >上传文件: <input type="file" name="file"/></p>
<input type="submit" value="上传"/>
</form>
</body>
</html>
- pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>test-openfeign</artifactId>
<groupId>com.xiyou</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test-feign</artifactId>
</project>
- application.yml
server:
port: 9000
spring:
application:
name: test-feign
eureka:
instance:
instance-id: server:1
client:
service-url:
default-zone: http://localhost:8761/eureka/
- 启动类(FeignApplication)
package com.xiyou.feign;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
public class FeignApplication {
public static void main(String[] args) {
SpringApplication.run(FeignApplication.class, args);
}
}
具体逻辑代码
- FeignController
package com.xiyou.feign.controller;
import com.xiyou.feign.service.FeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class FeignController {
@Autowired
private FeignService feignService;
@GetMapping("/toUploadPage")
public String toUploadPage(){
return "uploadFile.html";
}
@ResponseBody
@PostMapping("/upload")
public String upload(String filename, MultipartFile file){
feignService.fileDeal(file);
return "OK";
}
}
- FeignService
package com.xiyou.feign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
@FeignClient(value = "test-file-deal")
public interface FeignService {
@RequestMapping(value = "/dealFile", method = RequestMethod.POST,
produces = {MediaType.APPLICATION_JSON_UTF8_VALUE},
consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public String fileDeal(@RequestBody MultipartFile file);
}
总结:
我们在用OpenFeign实现文件上传的模块调用的时候,需要手动在@RequestMapping中指明consumes和produces