Spring mvc multipart

Jam*_*ame 6 java spring-mvc

我一直收到以下错误

org.springframework.web.multipart.support.MissingServletRequestPartException:未找到请求部分"模型".

将多部分请求发布到spring mvc controller时.

这是请求:

Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:4394941
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryK4y8t7mg2SNoYxC4
Cookie:SID=091f182f-5534-47c4-b0c1-8ca9c17e1f09
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/controller/home/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.57 Safari/537.17
X-Requested-With:XMLHttpRequest
Request Payload
------WebKitFormBoundaryK4y8t7mg2SNoYxC4
Content-Disposition: form-data; name="model"

{"name":"kjkjk","description":"kkjkjk"}
------WebKitFormBoundaryK4y8t7mg2SNoYxC4
Content-Disposition: form-data; name="photo"; filename="IMG_1281.JPG"
Content-Type: image/jpeg
Run Code Online (Sandbox Code Playgroud)

调节器

@RequestMapping(value = "/t")
    public ResponseEntity<ResponseMessage> t(@CookieValue(value = "SID", required = true) String sessionId, 
            @RequestPart("model") CategoryModel model,
            @RequestPart("photo") MultipartFile file)
    {
    return new ResponseEntity<ResponseMessage>(new ResponseMessage(200, "success"), HttpStatus.OK);
    }
Run Code Online (Sandbox Code Playgroud)

模型

package bla.bla.bla;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.codehaus.jackson.annotate.JsonBackReference;
import org.codehaus.jackson.annotate.JsonIgnore;

public class CategoryModel {

    public CategoryModel(String id, String name, String description, CategoryModel parent) {
        super();
        this.name = name;
        this.description = description;
        this.id = id;
        this.parent = parent;
    }

    public CategoryModel(String id, String name, String description, CategoryModel parent, List<CategoryModel> childrens) {
        super();
        this.name = name;
        this.description = description;
        this.id = id;
        this.parent = parent;
        this.childrens = childrens;
    }

    public CategoryModel()
    {

    }
    public String id;
    public String name;
    public String description;
    public String imageUrl; 
    public CategoryModel parent;
    public List<CategoryModel> childrens = new ArrayList<CategoryModel>();
}
Run Code Online (Sandbox Code Playgroud)

我添加了控制器和实体请检查并让我知道我哪里出错?

谢谢詹姆斯

Ale*_*lex 5

我也有类似的问题,幸运的是,这个答案帮助我弄清楚出了什么问题.正如那里提到的,问题不在你的Java部分.您必须更改在客户端构建CategoryModel的Javascript逻辑.根据该答案,您的逻辑应该看起来像下面显示的代码:

var file = ... // your file
var model = {
    id: 'TestId'
    name: 'TestName',
    description: 'TestDesciption',
    .... // other fields are ommited
};

var fd = new FormData();
fd.append('photo', file);
fd.append('model', new Blob([JSON.stringify(model)], { type: "application/json" }));
Run Code Online (Sandbox Code Playgroud)

使用此代码,您的例外应该得到修复.