<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # RESTEasy `ExceptionMapper` – 異常處理示例 > 原文: [https://howtodoinjava.com/resteasy/exception-handling-in-jax-rs-resteasy-with-exceptionmapper/](https://howtodoinjava.com/resteasy/exception-handling-in-jax-rs-resteasy-with-exceptionmapper/) 學習使用 resteasy `ExceptionMapper`接口實現創建和處理[自定義異常](https://howtodoinjava.com/java/exception-handling/best-practices-for-for-exception-handling/)。 `ExceptionMapper`是供應器的契約,該供應器將 [Java 異常](https://howtodoinjava.com/java/exception-handling/checked-vs-unchecked-exceptions-in-java/)映射到`Response`對象。 > 必須使用`@Provider`注解`ExceptionMapper`接口的實現才能正常工作。 ## 1\. Resteasy `ExceptionMapper` - 自定義異常處理器 `ExceptionMapper`的示例實現供應器類如下所示: ```java package com.howtodoinjava.exception; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; import javax.ws.rs.ext.ExceptionMapper; import javax.ws.rs.ext.Provider; @Provider public class MyApplicationExceptionHandler implements ExceptionMapper<MyApplicationException> { @Override public Response toResponse(MyApplicationException exception) { return Response.status(Status.BAD_REQUEST).entity(exception.getMessage()).build(); } } ``` 自定義異常類`MyApplicationException.java`的編寫方式為: ```java package com.howtodoinjava.exception; import java.io.Serializable; public class MyApplicationException extends Exception implements Serializable { private static final long serialVersionUID = 1L; public MyApplicationException() { super(); } public MyApplicationException(String msg) { super(msg); } public MyApplicationException(String msg, Exception e) { super(msg, e); } } ``` ## 2\. Resteasy REST API 為了測試`ExceptionMapper`實現,我編寫了以下 resteasy REST API。 ```java package com.howtodoinjava.rest; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.core.Response; import org.jboss.resteasy.spi.validation.ValidateRequest; import com.howtodoinjava.exception.MyApplicationException; @Path("/rest") public class UserService { @Path("/users/{id}") @GET @ValidateRequest public Response getUserBId ( @PathParam("id") String id ) throws MyApplicationException { //validate mandatory field if(id == null) { throw new MyApplicationException("id is not present in request !!"); } //Validate proper format try { Integer.parseInt(id); } catch(NumberFormatException e) { throw new MyApplicationException("id is not a number !!"); } //Process the request return Response.ok().entity("User with ID " + id + " found !!").build(); } } ``` ## 3\. RESTEasy `ExceptionMapper`演示 上面的 API 接受`Integer`格式的用戶`'id'`參數。 如果我們以無法解析為`Integer`的其他格式傳遞 id,則會拋出`MyApplicationException`。 我們的異常映射器應該能夠處理這個問題。 #### 3.1 有效請求 在瀏覽器中訪問`http://localhost:8080/RESTEasyExceptionMapperDemo/rest/users/1`。 ![Valid request to REST API](https://img.kancloud.cn/fd/2b/fd2b19d17a55df3f511b22c43b169bb7_632x401.png) REST API 的有效請求 #### 3.2 無效請求 - 引發異常 在瀏覽器中訪問`http://localhost:8080/RESTEasyExceptionMapperDemo/rest/users/abc`。 ![Invalid request to REST API](https://img.kancloud.cn/f5/59/f5591b2d56dcf122b79a96b93437f5c0_614x423.png) REST API 的無效請求 要使用`ExceptionMapper`接口下載此 **Resteasy 客戶端異常處理示例的源代碼**,請遵循以下給定的鏈接。 [源碼下載](https://docs.google.com/file/d/0B7yo2HclmjI4V0ZBZ2xqWllHMnc/edit?usp=sharing "jax-rs exception handling demo source code") 學習愉快!
                  <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>

                              哎呀哎呀视频在线观看