首页 > 其他分享 >9-springboot统一异常处理

9-springboot统一异常处理

时间:2023-03-22 17:59:32浏览次数:34  
标签:xml springboot 处理 hello 异常 class 统一

500错误页面之前可以xml中配置errorpage的配置,或者tomcat的web.xml中处理,现在可以进行统一处理。

新建处理类统一处理

@ControllerAdvice
public class ExceptionHandler {
@org.springframework.web.bind.annotation.ExceptionHandler(value = RuntimeException.class)
public @ResponseBody Object errorHandlerByJson(HttpServletRequest request,Exception e) throws Exception{
//可以拿到异常信息
e.printStackTrace();
//可以返回统一数据
return "服务器开小差";
}
}

@Controller
public class ExceptionController {
@RequestMapping("/hello")
public @ResponseBody String hello(){
int i = 10/0;
return "hello join";
}
}

 

 

 

标签:xml,springboot,处理,hello,异常,class,统一
From: https://www.cnblogs.com/healthinfo/p/17244910.html

相关文章

  • vue+element-ui+springboot实现修改当前登录用户的信息
    正文:话不多说,直接上代码springboot代码/***根据id修改当前登录用户的信息**@paramusername*@return*/@GetMapping("/userna......
  • 一款 SpringBoot 项目下最优雅的 HTTP 客户端工具RetrofitHttp
    大家都知道okhttp是一款由square公司开源的java版本http客户端工具。实际上,square公司还开源了基于okhttp进一步封装的retrofit工具,用来支持通过接口的方式发起http请求。......
  • springboot动态修改properties文件
    importjava.io.BufferedWriter;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStreamWriter;......
  • 29.自定义异常
    自定义异常publicclassMyExceptionextendsException{//继承//传递数>10抛出异常privateintdetail;publicMyException(inta){this.......
  • 28.捕获和抛出异常
    捕获和抛出异常五个关键字:try、catch、finally、throw、throwsinta=1;intb=0;try{//try可以监控区域System.out.println(a/b);}catch(Arithmeti......
  • 27.异常
    异常1.什么是异常?软件程序在运行过程中,有可能会出现各种问题,我们称之为异常,Exception。异常通常发生在程序运行期间,它会影响正常的程序执行流程。2.异常体系结构Java......
  • Nginx异常信息 upstream timed out (110: Connection timed out) while reading respo
    upstreamtimedout(110:Connectiontimedout)whilereadingresponseheaderfromupstreamNginx代理配置如下:###proxysettingsstartproxy_http_version1.1;p......
  • SpringBoot 第二部分知识点
    SpringBoot-第⼆天1.学习目标2Mybatis整合数据访问使用SpringBoot开发企业项目时,持久层数据访问是前端页面数据展示的基础,SpringBoot支持市面上常见的关系库......
  • java异常相关问题
    java中有哪些RunTimeException异常RuntimeException           是那些可能在Java虚拟机正常运行期间抛出的异常的超类,是所有运行时异常的顶级接......
  • SpringBoot 第一部分知识点
    SpringBoot-第⼀天1.学习目标 2.Spring框架发展史2.1.Spring1.x时代在Spring1.x时代,都是通过xml文件配置bean,随着项目的不断扩大,需要将xml配置分放到不......