2017-06-18 4 views
0

ReactJSアプリケーションの魔法使いからポストを送信しています。jsonオブジェクトマップとファイルがアップロードされています。いくつかのオブジェクトを持つPOST jsonとReactJSからRESTへのファイルSpringサーバー

どこ

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

私はFileReaderInputからFILE1を取得していますが、それは、HTMLの入力タイプのファイルからファイルとまったく同じです。このファイルには、フィールドlastModified、lastModifiedDate、name、size、type、webkitRelativePath、__proto_があります。

そして、私のRESTサーバー上の

私はこれを持っている:

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オブジェクトとヌルファイルの配列を送るよすべてが正常に動作します

@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; 
} 

。リクエストはCreateMapWrapperにマップされ、Mapとヌルの配列を出力します。

しかし、私は配列にファイルを配置することだ、私は例外を取得:

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: [email protected]; 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: [email protected]; line: 1, column: 577] (through reference chain: com.gismaps.CreateMapWrapper["files"]->java.util.HashSet[1]) 

間違っていますか?それはRESTでマッピングされたものですか、またはファイル形式を送信していますReact in wrong way?このようなものを投稿することはできますか?

私はおそらく何か愚かなことをしていますが、私はSpring RESTにファイルをアップロードすることを決してしていませんでした。

+0

方法MultipartFile Interfaceのインスタンスを作成する予定ですか? –

答えて

0

反応に固有の問題のようには見えません。マルチパートを処理するためにRESTエンドポイントを設定していないようです。あなたは、サーバー側でファイルのアップロードを処理する方法を参照するには春のために、このチュートリアルを見てみたいことがあり、次のように春休憩コントローラでフォームデータを取得するhttps://spring.io/guides/gs/uploading-files/

0

シンプルです:

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()); 
    } 
} 
関連する問題