java報錯資訊如何在頁面顯示

2020-10-21 12:00:23

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報錯資訊如何在頁面顯示的詳細內容,更多請關注TW511.COM其它相關文章!