加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – 不存在必需的MultipartFile参数 – Spring Boot REST

发布时间:2020-12-15 01:47:12 所属栏目:大数据 来源:网络整理
导读:我正在尝试进行概念验证,该概念涉及.Net系统将文件发布到Java Spring Boot应用程序上的Rest端点.我一直收到“必需参数不存在”错误.这个错误有很多SO问题,我尝试过那些没有运气的解决方案.谁能看到我做错了什么? 这是我的C#代码: private async Task 这是F

我正在尝试进行概念验证,该概念涉及.Net系统将文件发布到Java Spring Boot应用程序上的Rest端点.我一直收到“必需参数不存在”错误.这个错误有很多SO问题,我尝试过那些没有运气的解决方案.谁能看到我做错了什么?

这是我的C#代码:

    private async Task

这是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

编辑,因为我发布了错误的C#代码

最佳答案
好吧,也许我没有尝试过我在SO上看到的所有解决方案.

This question had a solution for me.

我不得不使用@ModelAttribute而不是@RequestParam.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读