带文件和JSON对象的多部分请求

时间:2015-09-01 15:16:31

标签: java json spring-mvc postman

我尝试创建将Multipart文件和json作为输入参数的函数。我写这样的功能,就是这样:

@RequestMapping(method = RequestMethod.PUT)
    public ResponseEntity<UserWithPhoto> update(@RequestHeader(value="Access-key") String accessKey,
                                         @RequestHeader(value="Secret-key") String secretKey,
                                         @RequestPart("user") String string,
                                         @RequestPart("photo") MultipartFile file) throws Exception{
            User user = new ObjectMapper().readValue(string, User.class);

但问题是无法测试。我像这样使用Postman客户端并收到错误org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided 我相信它因为我没有设置邮递员写enter image description here

1 个答案:

答案 0 :(得分:0)

您是否尝试过将MultipartResolver添加到Spring MVC配置中:

<bean id="multipartResolver"
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">