這篇文章主要介紹“Java通用異常處理舉例分析”,在日常操作中,相信很多人在Java通用異常處理舉例分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Java通用異常處理舉例分析”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
在江海等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站設(shè)計、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作按需制作網(wǎng)站,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計,營銷型網(wǎng)站建設(shè),外貿(mào)網(wǎng)站建設(shè),江海網(wǎng)站建設(shè)費用合理。
CommonExceptionHandler
import com.mouos.common.exception.MouosException; import com.mouos.common.vo.ExceptionResult; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; @ControllerAdvice public class CommonExceptionHandler { @ExceptionHandler(RuntimeException.class) public ResponseEntity<ExceptionResult> handleException(MouosException e) { return ResponseEntity.status(e.getExceptionEnum().getCode()).body(new ExceptionResult(e.getExceptionEnum())); } }
ExceptionEnum
import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @Getter @NoArgsConstructor @AllArgsConstructor public enum ExceptionEnum { PRICE_CONNOT_BE_NULL(400, "價格不能為空"); private int code; private String msg; }
MouosException
import com.mouos.common.enums.ExceptionEnum; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; @Getter @AllArgsConstructor @NoArgsConstructor public class MouosException extends RuntimeException { private ExceptionEnum exceptionEnum; }
ExceptionResult
import com.mouos.common.enums.ExceptionEnum; import lombok.Data; @Data public class ExceptionResult { private int status; private String message; private Long timestamp; public ExceptionResult(ExceptionEnum em) { this.status = em.getCode(); this.message = em.getMsg(); this.timestamp = System.currentTimeMillis(); } }
拋出異常
package com.mouos.item.web; import com.mouos.common.enums.ExceptionEnum; import com.mouos.common.exception.MouosException; import com.mouos.item.mode.Item; import com.mouos.item.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.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("item") public class ItemController { @Autowired private ItemService itemService; @PostMapping public ResponseEntity<Item> saveItem(Item item) { if (item.getPrice() == null) { throw new MouosException(ExceptionEnum.PRICE_CONNOT_BE_NULL); } return ResponseEntity.status(HttpStatus.CREATED).body(itemService.saveItem(item)); } }
到此,關(guān)于“Java通用異常處理舉例分析”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
網(wǎng)頁標題:Java通用異常處理舉例分析
本文鏈接:http://aaarwkj.com/article2/ijhjoc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站排名、微信公眾號、網(wǎng)站內(nèi)鏈、電子商務(wù)、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)