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报错信息如何在页面显示的详细内容,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)