2016-04-13 9 views
0

ファイルをダウンロードするためのSpringブートでrest APIを実装するために、これに続いてtutorial(xml形式)。次のようにHTTP 406レストコールでファイルをダウンロードする

私のコントローラクラスは次のとおりです。

@RestController 
public class RistoreController { 
    @Autowired 
    private RistoreService ristoreService; 

    @RequestMapping( 
      value = "/ristore/foundation/{trf}", 
      method = RequestMethod.GET, 
      produces = "application/xml") 
    public ResponseEntity<InputStream> getXMLById(@PathVariable("trf") String trf) throws IOException { 
     InputStream inputStream = ristoreService.findByTRF(trf); 
     return ResponseEntity 
       .ok() 
       .contentType(MediaType.parseMediaType("application/octet-stream")) 
       .body(inputStream); 
    } 
} 

私はそのサービスのためのコントローラとBeanクラスにautowiredサービスインタフェースRistoreServiceを持って次のようになります。私が使用してアプリケーションをテストし

@Service 
public class RistoreServiceBean implements RistoreService { 
    public InputStream findByTRF(String trf) throws IOException { 
     String filePath = "/Users/djiao/Box Sync/Work/Projects/RIStore/foundation/foundation_new/" + trf + ".xml"; 
     File file = new File(filePath); 
     return new FileInputStream(file); 
    } 
} 

カールコマンドに続いて:

curl -i -H "Accept: application/xml" http://localhost:8080/ristore/foundation/TRF133672_1455294493597 

ただし、私は406エラー、 "受け入れられない"を得た。ファイル形式に問題がありますか?あなたのコード内

答えて

1

その後方法

コントローラ
 @RequestMapping(value = "/ristore/foundation/{trf}", method = RequestMethod.GET, produces = "application/xml") 
    public ResponseEntity<InputStreamResource> downloadXMLFile(@PathVariable("trf") String trf) 
      throws IOException { 

     // Optinal headers configuration 

HttpHeaders headers = new HttpHeaders(); 

     headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); 
     headers.add("Pragma", "no-cache"); 
     headers.add("Expires", "0"); 


     // get the inputStream 

    InputStream xmlFileInputStream = ristoreService.findByTRF(trf); 



     return ResponseEntity 
       .ok() 
       .headers(headers) 
       .contentType(MediaType.parseMediaType("application/octet-stream")) 
       .body(new InputStreamResource(xmlFileInputStream)); 
    } 

あなたのサービスの定義を変更してみてくださいクラスは次のようになります:

@Service 
public class RistoreServiceBean implements RistoreService { 
    public InputStream findByTRF(String trf) throws IOException { 
     String filePath = "/Users/djiao/Box Sync/Work/Projects/RIStore/foundation/foundation_new/" + trf + ".xml"; 
     File file = new File(filePath); 
     return new FileInputStream(file); 
    } 
} 

406は受け入れられない要求によって識別されたリソースは、リクエストで送信受け入れるヘッダによれば受け入れられないコンテンツ特性を持つ応答エンティティを生成することができるだけです。

これは、返された入力ストリームは、RESTコントローラを持つとすぐにリソースとみなされる必要があることを意味します。

+0

これは動作します。メソッド 'downloadXMLFile'の引数に' @PathVariable( "trf")String trf'を追加しました。私はあなたが 'xmlFile'によって' xmlFileInputStream'を意味していたなら、 'InputStream'が' contentLength() 'メソッドを持っているとは思わない。 – ddd

+0

私は忘れた@pathVariableを追加する答えを編集しました。しかし、ここでの主な理由は、私が答えで説明した理由のためにResourceInputStreamが原因でした。ファイルの長さは正しいです。 私の場合、私はサービスからファイルを返し、コントローラ内のファイルを変換します。この場合、ファイルの長さを取得できます。答えを確認することを忘れないでください。 –

1

次の2行互いに矛盾:

  .contentType(MediaType.parseMediaType("application/octet-stream")) 

 produces = "application/xml") 
+0

これを 'contentType(MediaType.parseMediaType(" application/xml "))'に変更しても、同じエラーが表示されます。 – ddd

+0

実際には矛盾はありません。 XMLファイル(application/xml)の本体として集められた後、八重奏ストリームとしてコンテンツをレンタルする –

関連する問題