springboot springmvc抛出全局异常的解决方法
程序员文章站
2023-12-05 19:59:28
springboot中抛出异常,springboot自带的是springmvc框架,这个就不多说了。
springmvc统一异常解决方法这里要说明的是。只是结合了spri...
springboot中抛出异常,springboot自带的是springmvc框架,这个就不多说了。
springmvc统一异常解决方法这里要说明的是。只是结合了springboot的使用而已。直接上代码,有效有用的才是ok。
1.定义异常捕获
package com.example.rest.error; import org.springframework.http.httpstatus; import org.springframework.web.bind.annotation.exceptionhandler; import org.springframework.web.bind.annotation.responsestatus; import org.springframework.web.bind.annotation.restcontrolleradvice; import org.springframework.web.servlet.nohandlerfoundexception; import javax.validation.constraintviolationexception; /** * * @author ming 定义全局异常处理 * @restcontrolleradvice 是@controlleradvice 与@responsebody 的组合注解 */ @restcontrolleradvice public class globalcontrollerexceptionhandler { @exceptionhandler(value = { constraintviolationexception.class }) @responsestatus(httpstatus.bad_request) public apierrorresponse constraintviolationexception(constraintviolationexception ex) { return new apierrorresponse(500, 5001, ex.getmessage()); } @exceptionhandler(value = { illegalargumentexception.class }) @responsestatus(httpstatus.bad_request) public apierrorresponse illegalargumentexception(illegalargumentexception ex) { return new apierrorresponse(501, 5002, ex.getmessage()); } @exceptionhandler(value = { nohandlerfoundexception.class }) @responsestatus(httpstatus.not_found) public apierrorresponse nohandlerfoundexception(exception ex) { return new apierrorresponse(404, 4041, ex.getmessage()); } @exceptionhandler(value = { exception.class }) @responsestatus(httpstatus.internal_server_error) public apierrorresponse unknownexception(exception ex) { return new apierrorresponse(500, 5002, ex.getmessage()); } }
2.定义一个返回对象
package com.example.rest.error; /** * @author ming */ public class apierrorresponse { private int status; private int code; private string message; public apierrorresponse(int status, int code, string message) { this.status = status; this.code = code; this.message = message; } public int getstatus() { return status; } public int getcode() { return code; } public string getmessage() { return message; } @override public string tostring() { return "apierrorresponse{" + "status=" + status + ", code=" + code + ", message=" + message + '}'; } }
3.定义一个启动application
package com.example; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; import org.springframework.web.servlet.config.annotation.enablewebmvc; @springbootapplication @enablewebmvc public class springbootexceptionhandlingapplication { public static void main(string[] args) { springapplication.run(springbootexceptionhandlingapplication.class, args); } }
4.最后一个测试类
package com.example.rest.controller; import org.springframework.http.mediatype; import org.springframework.util.assert; import org.springframework.web.bind.annotation.getmapping; import org.springframework.web.bind.annotation.restcontroller; import javax.validation.constraintviolationexception; import java.util.collections; /** * @author ming */ @restcontroller public class testcontroller { @getmapping(value = "/test", produces = mediatype.application_json_value) public void test(long id) { assert.notnull(id,"id不能为空!"); throw new constraintviolationexception("error", collections.emptyset()); } }
注意application.properties这个文件的配置
spring.mvc.throw-exception-if-no-handler-found=true
ok,springboot中解决springmvc异常抛出就可以这样解决了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Java中类赋值的解释实例详解
下一篇: 详解VUE项目中安装和使用vant组件