<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Jersey 異常處理 – Jersey `ExceptionMapper`示例 > 原文: [https://howtodoinjava.com/jersey/jaxrs-jersey-exceptionmapper/](https://howtodoinjava.com/jersey/jaxrs-jersey-exceptionmapper/) 在 Jersey `ExceptionMapper`示例中,我們將學習在開發 Jersey RESTful Web 服務時使用[`ExceptionMapper`](https://docs.oracle.com/javaee/7/api/javax/ws/rs/ext/ExceptionMapper.html)接口處理**自定義異常**。 出于演示目的,我正在修改為[Jersey 下載文件示例](//howtodoinjava.com/jersey/jax-rs-jersey-2-file-download-example-using-streamingoutput/)編寫的源代碼。 ```java Table Of Contents 1\. Jersey custom exception with ExceptionMapper 2\. How to throw exception from REST API 3\. Demo ``` ## 1\. Jersey `ExceptionMapper` – 創建自定義異常 要在基于 JAX-RS 的 Web 服務中處理自定義異常,您應該創建一個異常類,然后實現`ExceptionMapper`接口。 ```java package com.howtodoinjava.jersey; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; @Provider public class MissingFileException extends Exception implements ExceptionMapper<MissingFileException> { private static final long serialVersionUID = 1L; public MissingFileException() { super("No File found with given name !!"); } public MissingFileException(String string) { super(string); } @Override public Response toResponse(MissingFileException exception) { return Response.status(404).entity(exception.getMessage()) .type("text/plain").build(); } } ``` ## 2\. 如何從 REST API 引發異常 現在,如果在所需位置找不到用戶請求的文件,則可以拋出`MissingFileException`。 ```java package com.howtodoinjava.jersey; import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.StreamingOutput; @Path("/download") public class JerseyService { @GET @Path("/{fileName}") public Response downloadPdfFile(final @PathParam("fileName") String fileName) throws MissingFileException { final String fullFilePath = "C:/temp/" + fileName; File file = new File(fullFilePath); if(file.exists() == false){ throw new MissingFileException(fileName + " does not existing on this server !!"); } StreamingOutput fileStream = new StreamingOutput() { @Override public void write(java.io.OutputStream output) throws IOException { try { java.nio.file.Path path = Paths.get(fullFilePath); byte[] data = Files.readAllBytes(path); output.write(data); output.flush(); } catch (IOException e) { throw new IOException("Error while reading file :: '"+fileName+"' !!"); } } }; return Response .ok(fileStream, MediaType.APPLICATION_OCTET_STREAM) .header("content-disposition","attachment; filename = '"+fileName) .build(); } } ``` ## 3\. Jersey 異常處理示例 現在該測試 Jersey 異常映射器了。 現在,讓我們看看找不到文件時會發生什么。 #### 3.1 當用戶要求正確的文件時 ![No exception when file is found](https://img.kancloud.cn/2c/8f/2c8f284edd40e07767f2eee51b97fe0f_700x350.png) 找到文件時沒有異常 #### 3.2 當用戶要求提供未知文件時 ![404 with custom message when file is not found](https://img.kancloud.cn/32/e9/32e9018b52acee8db935a4bb72ef8cd9_700x351.png) 找不到文件時帶有自定義消息的 404 #### 3.3 未捕獲的異常處理 如果要在進入用戶屏幕之前處理所有未捕獲的異常,則必須映射`Throwable`本身。 ```java package com.howtodoinjava.jersey.provider; import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; @Provider public class UncaughtException extends Throwable implements ExceptionMapper<Throwable> { private static final long serialVersionUID = 1L; @Override public Response toResponse(Throwable exception) { return Response.status(500).entity("Something bad happened. Please try again !!").type("text/plain").build(); } } ``` 請問您有關 *jaxrs 異常映射器示例*的問題。 學習愉快!
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看