簡體   English   中英

從ReactJS到REST Spring服務器的JSON帶有一些對象和文件

[英]POST json with some object and File from ReactJS to REST Spring server

我正在從我的ReactJS應用程序發送帖子,其中包含一些json對象Map和用戶上傳的文件。

axios.post(`http://localhost:8080/api/maps`, this.state)

哪里

this.state = {map: {title: "Title", layers: [...] etc.}, files: [file1]}

我從FileReaderInput獲取file1,但它與html的輸入類型文件中的文件完全相同。 該文件具有字段lastModified,lastModifiedDate,名稱,大小,類型,webkitRelativePath,__ proto_。

在我的REST服務器上,我有這個:

@RequestMapping(value = "/maps", method = RequestMethod.POST)
public @ResponseBody
HttpStatus add(@RequestBody CreateMapWrapper wrapper) throws IOException, InterruptedException {
    System.out.println(wrapper.getMap());
    System.out.println(wrapper.getFiles());
    return HttpStatus.OK;
}

哪里

public class CreateMapWrapper {
    private com.gismaps.pojos.Map map;
    private Set<MultipartFile> files;

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public Set<MultipartFile> getFiles() {
        return files;
    }

    public void setFiles(Set<MultipartFile> files) {
        this.files = files;
    }
}

當我發送Map對象和空文件數組時,一切正常。 請求被映射到CreateMapWrapper並打印Map和null數組。

但是當我將文件放置到數組時,出現異常:

WARN 4348 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON document: Can not construct instance of org.springframework.web.multipart.MultipartFile: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: java.io.PushbackInputStream@79e43652; line: 1, column: 577] (through reference chain: com.gismaps.CreateMapWrapper["files"]->java.util.HashSet[1]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.springframework.web.multipart.MultipartFile: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
 at [Source: java.io.PushbackInputStream@79e43652; line: 1, column: 577] (through reference chain: com.gismaps.CreateMapWrapper["files"]->java.util.HashSet[1])

怎么了? 是在REST中映射,還是我以錯誤的方式發送文件形式React? 我什至可以發布這樣的內容嗎?

我可能在做一些愚蠢的事情,但是我從來沒有將文件上傳到Spring REST。

看起來不像是特定於反應的問題。 看起來您沒有設置REST端點來處理多部分。 您可能需要查看Spring的本教程,以了解如何在服務器端處理文件上傳: https : //spring.io/guides/gs/uploading-files/

在Spring Rest Controller中獲取表單數據的簡單方法如下:

RequestMapping(value = "/maps", method = RequestMethod.POST)
public @ResponseBody Object uploadFiles(MultipartHttpServletRequest request, HttpServletResponse response) throws IOException {
    //get form data fields
    final String field= request.getParameter('fieldName');
    //and so on......

    //Now get the files.
    Iterator<String> iterator = request.getFileNames();
    MultipartFile multipartFile = null;
    while (iterator.hasNext()) {
        multipartFile = request.getFile(iterator.next());
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM