java报错信息如何在页面显示
导读
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; }
通过以上五步,就可以把后台抛出的异常信息,在前端页面显示了
评论(0)