Using Spring OpenFeign to transfer files

Posted by vijay17877 on Tue, 17 Mar 2020 08:02:09 +0100

Custom Catalog title is written here

Module B uploads the file and calls the file saving implementation of module A

1.1 business scenario

First, the interface and implementation of uploading and saving files are written in module A,
Secondly, in the development of module B, the interface of module A needs to be called
A module interface uses form data to transfer file files
When module B calls interface A with OpenFeign, an exception occurs:

	feign.FeignException: status 400 reading xxx

2.1 troubleshooting process

A module interface is normal
Module B accepts normally with form data
Module B calls the interface of module A, which is abnormal when passing parameters, because form data is not used when calling OpenFeign

3.1 solutions

Method: when using OpenFeign to call module A interface, module B needs to make the following changes

  1. Specify consumers in @ PostMapping() (see Chapter 4.1 for details)
  2. file use @ RequestPart annotation
@ApiOperation(value = "Add topology CAD Design drawings")
@PostMapping(path = "/design/file/target/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
DesignFile addDesignFile(@PathVariable("id") Integer targetId,
                         @RequestParam(value = "targetType") String targetType,
                         @RequestPart("file") MultipartFile file);

Note in this Code:
Must be a request of type Post.
The file parameter must be decorated with @ RequestPara MultipartFile file, where @ requestpara and MultipartFile are indispensable.

4.1 knowledge expansion

4.1.1 @PostMapping parameter meaning

@PostMapping is an annotation that describes post requests
@PostMapping is equivalent to @ RequestMapping(method = RequestMethod.POST)

parameter Meaning
path Request path
consumes Content format for Request Submission
method Request type

4.1.2 interface parameter description

parameter Meaning
@Pathvariable (parameter name) type variable name The parameter is written in the URL, and the parameter in the URL occupation
@RequestParam(value = "parameter name") type variable name Parameter is written in body, which is the parameter after "in URL"
@RequestPart("parameter name") MultipartFile variable name It is especially used when transferring files. It is a parameter to modify files

Complete code

1 introduce OpenFeign dependency

 <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form</artifactId>
            <version>xxx</version>
        </dependency>

        <dependency>
            <groupId>io.github.openfeign.form</groupId>
            <artifactId>feign-form-spring</artifactId>
            <version>xxx</version>
        </dependency>

Module A (provider)

controller layer

@RestController
public class DesignFileController {
	@SneakyThrows
	@ApiOperation(value = "Add topology CAD Design drawings")
	@PostMapping("/design/file/target/{id}")
	public DesignFile addDesignFile(@PathVariable("id") Integer targetId,
	                                @RequestParam(value = "targetType") String targetType,
	                                @RequestParam("file") MultipartFile file) {
	    val fileName = file.getOriginalFilename();
	   return  this.designFileService.addDesignFile(targetId, fileName, description, targetType);
	}
}

B module (caller)

service level

/**
 * @Author: yibo
 * @Date: 2020-03-17 11:30
 * @Description: Topology attachment
 */
 @FeignClient(
        name = "multipart-support-service",
        url = "http://localhost:8080",
        configuration = Client.ClientConfiguration.class
)
public interface GraphService {
    @ApiOperation(value = "Add topology CAD Design drawings")
    @PostMapping(path = "/design/file/target/{id}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    DesignFile addDesignFile(@PathVariable("id") Integer targetId,
                             @RequestParam(value = "targetType") String targetType,
                             @RequestParam(value = "description", required = false) String description,
                             @RequestPart("file") MultipartFile file);
}

If you have any questions, you can send them to me personally, or comment on them. You will reply when you see them

122 original articles published, praised 23, visited 30000+
Private letter follow

Topics: github Spring