大佬教程收集整理的这篇文章主要介绍了java – 不存在必需的MultipartFile参数 – Spring Boot REST POST,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试进行概念验证,该概念涉及.Net系统将文件发布到Java Spring Boot应用程序上的Rest端点.我一直收到“必需参数不存在”错误.这个错误有很多SO问题,我尝试过那些没有运气的解决方案.谁能看到我做错了什么?
这是我的C#代码:
private async Task stem.IO.FileStream fileStream)
{
using (var client = _httpClientFactory())
{
using (var content = new MultipartFormDataContent())
{
content.Add(new StreamContent(fileStream),"assetFile");
using (var message = await client.PostAsync(uri,content))
{
return await message.Content.ReadAsStringAsync();
}
}
}
}
这是fiddler看到的请求:
POST http://10.0.2.2:8083/asset/1000/1001 http/1.1
Content-Type: multipart/form-data; boundary="BAC9aebd-d9ff-40ef-bcf3-4fffdd1b2c00"
Host: 10.0.2.2:8083
Content-Length: 158
Expect: 100-conTinue
Connection: Keep-Alive
--BAC9aebd-d9ff-40ef-bcf3-4fffdd1b2c00
Content-Disposition: form-data; name=assetFile
foo,bar,10
foo2,bar2,12
--BAC9aebd-d9ff-40ef-bcf3-4fffdd1b2c00--
这是我的控制器:
@RestController
@requestMapping("/asset/")
public class AssetController {
@requestMapping(path="{merchantID}/{assetID}",method=requestMethod.POST)
public String getAsset(
httpServletrequest request,@requestParam("assetFile") multipartfile file,@PathVariable("merchantId") long merchantId,@PathVariable("assetId") long assetId) throws IOException
{
return "It worked!";
}
}
这是我的配置:
@SpringBootApplication(exclude={MultipartAutoConfiguration.class})
public class MySpringApplication {
public static void main(String[] args) {
SpringApplication.run(MySpringApplication.class,args);
}
@Bean(name = "multipartResolver")
public CommonsMultipartResolver multipartResolver() {
System.out.println("multipartResolver()");
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
return multipartResolver;
}
}
以下是回复:
http/1.1 400 Bad request
Server: Apache-Coyote/1.1
Content-Type: application/json;charset=UTF-8
transfer-encoding: chunked
Date: Fri,25 Mar 2016 19:34:55 GMT
Connection: close
f3
{"timestamp":1458934495566,"status":400,"error":"Bad request","exception":"org.springframework.web.bind.MissingServletrequestParameterException","message":"required multipartfile parameter 'assetFile' is not present","path":"/asset/1000/1001"}
0
This question had a solution for me.
我不得不使用@modelattribute而不是@requestParam.
以上是大佬教程为你收集整理的java – 不存在必需的MultipartFile参数 – Spring Boot REST POST全部内容,希望文章能够帮你解决java – 不存在必需的MultipartFile参数 – Spring Boot REST POST所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。