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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 代碼 如下,spring默認幫我們處理的異常,很不靠譜.需要我們自己去寫. ~~~ package com.like.domain; public class Item { private Integer id; private String name; private Double price; @Override public String toString() { return "Item{" + "id=" + id + ", name='" + name + '\'' + ", price=" + price + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } } ~~~ ~~~ package com.like.service; import com.like.domain.Item; import org.springframework.stereotype.Service; import java.util.Random; @Service public class ItemService { public Item saveItem(Item item) { int id = new Random().nextInt(100); item.setId(id); return item; } } ~~~ ~~~ package com.like.controller; import com.like.domain.Item; import com.like.service.ItemService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/item") public class ItemController { @Autowired private ItemService itemService; @PostMapping("/show") public ResponseEntity<Item> saveItem(Item item) { if (item.getPrice() == null){ throw new RuntimeException("價格不能為空"); } return ResponseEntity.status(HttpStatus.CREATED).body(itemService.saveItem(item)); } } ~~~ ## 結果 ![](https://box.kancloud.cn/6b846a754522c808c168b55f25e03b1d_2022x970.png) ## 返回異常 創建CommonExceptionHandler類. ~~~ package com.like.advice; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @ControllerAdvice //默認情況下它會自動攔截所有的@controller注解的類.注意,要保證這個類能被啟動類掃描到并防區容器中 public class CommonExceptionHandler { @ExceptionHandler(RuntimeException.class) //攔截的異常類型 public ResponseEntity<String> handleException(RuntimeException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); } } ~~~ ## 結果 ![](https://box.kancloud.cn/8cc01eba8e1e55d9271efbfec153d423_1546x762.png) ## 自定義異常類
                  <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>

                              哎呀哎呀视频在线观看