![](/img/trans.png)
[英]RestTemplate Could not write request: no suitable HttpMessageConverter found for request type [java.util.HashMap]
[英]no suitable HttpMessageConverter found for request type [java.util.LinkedHashMap] and content type [multipart/form-data]
我試圖集成一個文件上傳服務,該服務已在 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));
}
我仍然遇到所有三種方法的例外情況:
上傳使用PUT:
無法寫入請求:找不到適合請求類型 [java.io.File] 和內容類型 [multipart/form-data] 的 HttpMessageConverter
上傳使用PUT1:
無法寫入請求:找不到適合請求類型 [java.util.LinkedHashMap] 和內容類型 [multipart/form-data] 的 HttpMessageConverter
上傳使用PUT2:
所需的請求部分“文件”不存在
請建議
這個問題現在似乎已經解決了,我在 2.0.x 版本的 feign-form 上,當我升級到 3.4.1 時它開始工作了。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.