java報錯資訊在頁面顯示的方法:先自訂異常類別;然後在service層的拋出異常類,controller類繼承主異常類;接著定義的主異常類,裡面可以寫多個自己定義的異常類別;最後完成訊息類別代碼。
java報錯資訊在頁面顯示的方法:
1、自訂異常類別
public class ProdProcessOrderException extends RuntimeException { private static final long serialVersionUID = 1L; public ProdProcessOrderException(String message) { super(message); } }
2、service層的程式碼
@Service public class GeneralDpPpOrderServiceImpl implements GeneralDpPpOrderService { public ResponseEntity<byte[]> downLoadExcel(GeneralDpPpOrder generalDpPpOrder) { //......... //省略代码 if (pOrderRouting == null) { //此处抛出异常 throw new ProdProcessOrderException("当前门板流程单没有设置生产工艺."); } } }
service層的拋出例外類別
3、controller層的程式碼
@RestController @RequestMapping("/api/mes/gene-dp-pp-order") public class GeneralDpPpOrderController extends ExceptionResponse{ //继承主异常类 @Autowired private GeneralDpPpOrderService generalDpPpOrderService; @RequestMapping(value = "/download-excel", method = RequestMethod.PUT, consumes = "application/json;charset=UTF-8", produces = "application/xls") public ResponseEntity<byte[]> downLoadExcel(@RequestBody GeneralDpPpOrder gdp) { return generalDpPpOrderService.downLoadExcel(gdp); }
controller類別繼承主異常類別
4、主異常類別代碼
public abstract class ExceptionResponse { @ExceptionHandler(ProdProcessOrderException.class) @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) public SystemMessage prodProcessOrderException(ProdProcessOrderException e) { return new SystemMessage(500, e.getMessage()); } }
定義的主異常類,裡面可以寫多個自己定義的異常類別
5、訊息類別代碼
public class SystemMessage { private int code; private String message; public SystemMessage(int code, String message) { super(); this.code = code; this.message = message; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; }
透過以上五步,就可以把後台拋出的異常訊息,在前端頁面顯示了
#相關學習推薦:##java基礎教學
以上是java報錯資訊如何在頁面顯示的詳細內容。更多資訊請關注PHP中文網其他相關文章!