openfeign はファイルをダウンロードするためのクライアントをどのように宣言しますか?

ファイル ストリームを返す機能を提供する API サービスがあります。インターフェイスは次のとおりです。

/**
 * 下载文件
 * type
 * 0 原文
 * 1 译文
 * 2 双语
 * 3 解析
 * @param type
 * @param fileUuid
 * @return
 */
@RequestMapping(value = "/downloadFile", method = {
    
    RequestMethod.POST, RequestMethod.GET})
public ResponseEntity<FileSystemResource> downloadFile(
										@NotNull(message = "请选择文件类型") Integer type,
										@NotNull(message = "fileUuid不允许为空") String fileUuid) {
    
    
}

このインターフェイスは通常、ファイルを返しますが、ファイルが存在しない場合は、ファイルが存在しないことを示す json 文字列を返します。次の 2 つの方法があります。

  1. 指定した例外をスローし、例外インターセプターを使用して一律に処理する

  2. 次のように、 null 型を返しResponseEntity<FileSystemResource>、同時に受信しHttpServletResponse response、エラー情報を応答に書き込みます。

    @RequestMapping(value = "/downloadFile", method = {
          
          RequestMethod.POST, RequestMethod.GET})
    public ResponseEntity<FileSystemResource> downloadFile(@NotNull(message = "请选择文件类型") Integer type,
                                                           @NotNull(message = "fileUuid不允许为空") String fileUuid,
                                                           HttpServletResponse response
        try {
          
          
            response.setContentType("application/json;charset=UTF-8");
            response.addHeader("Access-Control-Allow-Origin", "*");
            response.getWriter().write("{code:500,msg:\"文件不存在1\",data:null}");
        } catch (IOException e) {
          
          
            e.printStackTrace();
        }
        // return new ResponseEntity<>(null, null, HttpStatus.OK);
        return null;
    }
    

上記のインターフェースが内部でのみ使用され、宣言型のクライアントが必要な場合は、どうすればよいですか? 受け取る必要があるのは 1 つだけです ResponseEntity<Resource>

@FeignClient(name = "edniutrans-document-api", url = "${edniutrans.document.api-url}")
public interface DocumentApiFeignService {
    
    


    /**
     * 下载文件
     * type
     * 0 原文
     * 1 译文
     * 2 双语
     * 3 解析
     */
    @RequestMapping(value = "/documentTrans/downloadFile", method = {
    
    RequestMethod.POST})
    ResponseEntity<Resource> downloadFile(
            @RequestParam(value = "type") Integer type,
            @RequestParam(value = "fileUuid") String fileUuid
    );

おすすめ

転載: blog.csdn.net/weixin_43702146/article/details/129295178