我正在尝试集成文件上传服务,该服务已在Eureka发现基础结构中注册。
我的服务,说/ myfile / upload具有以下6个参数,以下是YML:
/myfile/upload:
put:
operationId: "uploadUsingPUT"
consumes:
- "multipart/form-data"
produces:
- "*/*"
parameters:
- name: "file"
in: "formData"
required: true
type: "file"
- name: "filename"
in: "formData"
required: true
type: "string"
- name: "path"
in: "formData"
required: true
type: "string"
- name: "header1"
in: "header"
required: true
type: "string"
- name: "header2"
in: "header"
required: false
type: "string"
allowEmptyValue: true
responses:
200:
description: "OK"
400:
description: "Bad Request"
我已经为此服务创建了一个客户端接口,下面是我创建的API:
import java.io.File;
import java.util.Map;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
@org.springframework.cloud.netflix.feign.FeignClient(value = "SERVICE-NAME", configuration = {
com.MyConfiguration.class})
public interface UploadControllerAPINew extends ApiClient.Api {
@RequestMapping(value = "/myfile/upload",
method = RequestMethod.PUT,
produces = "*/*",
consumes = "multipart/form-data"
)
FileUploadResponse uploadUsingPUT(@RequestPart("file") File file,
@RequestParam("filename") String filename, @RequestParam("path") String path,
@RequestHeader("header1") String header1,
@RequestHeader("header2") String header2);
@RequestMapping(value = "/myfile/upload",
method = RequestMethod.PUT,
produces = "*/*",
consumes = "multipart/form-data"
)
FileUploadResponse uploadUsingPUT1(@RequestBody Map<String, ?> formParams,
@RequestHeader("header1") String header1,
@RequestHeader("header2") String header2);
@RequestMapping(value = "/myfile/upload",
method = RequestMethod.PUT,
produces = "*/*",
consumes = "multipart/form-data"
)
FileUploadResponse uploadUsingPUT2(@RequestPart("file") byte[] file,
@RequestParam("filename") String filename, @RequestParam("path") String path,
@RequestHeader("header1") String header1,
@RequestHeader("header2") String header2);
}
为它提供编码器,我在编码器下面添加了:
@Bean
public Encoder feignEncoder() {
ObjectFactory<HttpMessageConverters> objectFactory = () ->
new HttpMessageConverters(new FormHttpMessageConverter());
// return new SpringEncoder(objectFactory);
return new FormEncoder(new SpringEncoder(objectFactory));
}
这三种方法仍然有例外:
uploadUsingPUT:
无法写入请求:找不到适合的HttpMessageConverter 请求类型[java.io.File]和内容类型[multipart / form-data]
uploadUsingPUT1:
无法写入请求:找不到适合的HttpMessageConverter 请求类型[java.util.LinkedHashMap]和内容类型 [multipart / form-data]
uploadUsingPUT2:
不存在所需的请求部分“文件”
请建议
答案 0 :(得分:0)
这个问题现在似乎已经解决,当我升级到3.4.1版本时,我使用的是伪装形式的2.0.x版本。