From fab19e90c98f44508207b6b2b0bb7b31513beeab Mon Sep 17 00:00:00 2001 From: aoshiguchen <1052045476@qq.com> Date: Fri, 29 Jul 2022 07:27:12 +0800 Subject: [PATCH] =?UTF-8?q?web=E6=94=AF=E6=8C=81=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E3=80=81=E5=85=A8=E5=B1=80?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E5=A4=84=E7=90=86.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../neutrino/core/util/HttpServerUtil.java | 17 +++++-- .../neutrino/core/web/HttpRequestHandler.java | 29 ++++++++--- .../core/web/config/WebMvcConfigurer.java | 10 ++++ .../core/web/context/WebContextHolder.java | 19 ++++++- .../interceptor/ExceptionHandlerRegistry.java | 42 ++++++++++++++++ .../RestControllerAdviceHandler.java | 45 +++++++++++++++++ .../RestControllerExceptionHandler.java | 48 ++++++++++++++++++ .../core/web/test1/GlobalAdviceHandler.java | 47 +++++++++++++++++ .../web/test1/GlobalExceptionHandler.java | 50 +++++++++++++++++++ .../neutrino/core/web/test1/JsonResult.java | 39 +++++++++++++++ .../core/web/test1/WebConfigurerAdapter.java | 12 +++++ 11 files changed, 347 insertions(+), 11 deletions(-) create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/ExceptionHandlerRegistry.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/RestControllerAdviceHandler.java create mode 100644 neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/RestControllerExceptionHandler.java create mode 100644 neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/GlobalAdviceHandler.java create mode 100644 neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/GlobalExceptionHandler.java create mode 100644 neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/JsonResult.java diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/HttpServerUtil.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/HttpServerUtil.java index 460061e2..1adc26b0 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/HttpServerUtil.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/util/HttpServerUtil.java @@ -13,6 +13,7 @@ package fun.asgc.neutrino.core.util; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import fun.asgc.neutrino.core.web.HttpResponseEntry; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFutureListener; @@ -28,19 +29,27 @@ import org.apache.commons.lang3.exception.ExceptionUtils; public class HttpServerUtil { public static void send404Response(ChannelHandlerContext context, String url) { - HttpResponseEntry responseEntry = new HttpResponseEntry(HttpResponseStatus.NOT_FOUND.code(), HttpResponseStatus.NOT_FOUND.reasonPhrase(), String.format("未找到指定资源: %s", url)); - String res = JSON.toJSONString(responseEntry); + String res = String.format("404 未找到指定资源: %s", url); FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.NOT_FOUND, Unpooled.wrappedBuffer(res.getBytes())); fullHttpResponse.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON); context.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.CLOSE); } public static void send500Response(ChannelHandlerContext context, Throwable throwable) { - HttpResponseEntry responseEntry = new HttpResponseEntry(HttpResponseStatus.INTERNAL_SERVER_ERROR.code(), HttpResponseStatus.INTERNAL_SERVER_ERROR.reasonPhrase(), String.format("服务异常: %s", ExceptionUtils.getStackTrace(throwable))); - String res = JSON.toJSONString(responseEntry); + String res = String.format("500 服务异常: %s", ExceptionUtils.getStackTrace(throwable)); FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.INTERNAL_SERVER_ERROR, Unpooled.wrappedBuffer(res.getBytes())); fullHttpResponse.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON); context.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.CLOSE); } + public static void send200Response(ChannelHandlerContext context, Object o) { + String res = String.valueOf(o); + if (null != o && !TypeUtil.isNormalBasicType(o.getClass())) { + res = JSONObject.toJSONString(o); + } + FullHttpResponse fullHttpResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, Unpooled.wrappedBuffer(res.getBytes())); + fullHttpResponse.headers().add(HttpHeaderNames.CONTENT_TYPE, HttpHeaderValues.APPLICATION_JSON); + context.writeAndFlush(fullHttpResponse).addListener(ChannelFutureListener.CLOSE); + } + } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java index a69b4684..853a60d2 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/HttpRequestHandler.java @@ -33,10 +33,7 @@ import fun.asgc.neutrino.core.web.annotation.RequestParam; import fun.asgc.neutrino.core.web.context.HttpContextHolder; import fun.asgc.neutrino.core.web.context.HttpRequestParser; import fun.asgc.neutrino.core.web.context.WebContextHolder; -import fun.asgc.neutrino.core.web.interceptor.HandlerInterceptor; -import fun.asgc.neutrino.core.web.interceptor.InterceptorRegistration; -import fun.asgc.neutrino.core.web.interceptor.InterceptorRegistry; -import fun.asgc.neutrino.core.web.interceptor.MappedInterceptor; +import fun.asgc.neutrino.core.web.interceptor.*; import fun.asgc.neutrino.core.web.router.DefaultHttpRouter; import fun.asgc.neutrino.core.web.router.HttpRouteParam; import fun.asgc.neutrino.core.web.router.HttpRouteResult; @@ -92,6 +89,11 @@ public class HttpRequestHandler { } Object invokeResult = invoke(httpRouteResult.getInstance(), httpRouteResult.getMethod()); + + if (null != WebContextHolder.getAdviceHandler()) { + invokeResult = WebContextHolder.getAdviceHandler().advice(context, requestParser, routePath, httpRouteResult.getMethod(), invokeResult); + } + String res = String.valueOf(invokeResult); if (null != invokeResult && !TypeUtil.isNormalBasicType(invokeResult.getClass())) { res = JSONObject.toJSONString(invokeResult); @@ -126,7 +128,10 @@ public class HttpRequestHandler { return; } } catch (Throwable e) { - exceptionHandler(e); + Object res = exceptionHandler(e); + if (null != res) { + HttpServerUtil.send200Response(context, res); + } } finally { release(); } @@ -170,9 +175,21 @@ public class HttpRequestHandler { return result; } - private void exceptionHandler(Throwable e) { + private Object exceptionHandler(Throwable e) { + ExceptionHandlerRegistry exceptionHandlerRegistry = WebContextHolder.getExceptionHandlerRegistry(); + if (CollectionUtil.isEmpty(exceptionHandlerRegistry.getExceptionHandlerList())) { + log.error("Http处理异常", e); + HttpServerUtil.send500Response(HttpContextHolder.getChannelHandlerContext(), e); + return null; + } + for (RestControllerExceptionHandler exceptionHandler : exceptionHandlerRegistry.getExceptionHandlerList()) { + if (exceptionHandler.support(e)) { + return exceptionHandler.handle(HttpContextHolder.getChannelHandlerContext(), HttpContextHolder.getHttpRequestParser(), e); + } + } log.error("Http处理异常", e); HttpServerUtil.send500Response(HttpContextHolder.getChannelHandlerContext(), e); + return null; } private Object invoke(Object instance, Method method) throws InvocationTargetException, IllegalAccessException { diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/config/WebMvcConfigurer.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/config/WebMvcConfigurer.java index b7cf5955..a72b5143 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/config/WebMvcConfigurer.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/config/WebMvcConfigurer.java @@ -21,7 +21,9 @@ */ package fun.asgc.neutrino.core.web.config; +import fun.asgc.neutrino.core.web.interceptor.ExceptionHandlerRegistry; import fun.asgc.neutrino.core.web.interceptor.InterceptorRegistry; +import fun.asgc.neutrino.core.web.interceptor.RestControllerAdviceHandler; /** * @@ -33,4 +35,12 @@ public interface WebMvcConfigurer { default void addInterceptors(InterceptorRegistry registry) { } + + default void addExceptionHandler(ExceptionHandlerRegistry registry) { + + } + + default RestControllerAdviceHandler adviceHandler() { + return null; + } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/context/WebContextHolder.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/context/WebContextHolder.java index cd0d436e..a5231add 100644 --- a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/context/WebContextHolder.java +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/context/WebContextHolder.java @@ -31,7 +31,9 @@ import fun.asgc.neutrino.core.context.ApplicationConfig; import fun.asgc.neutrino.core.util.*; import fun.asgc.neutrino.core.web.annotation.RestController; import fun.asgc.neutrino.core.web.config.WebMvcConfigurer; +import fun.asgc.neutrino.core.web.interceptor.ExceptionHandlerRegistry; import fun.asgc.neutrino.core.web.interceptor.InterceptorRegistry; +import fun.asgc.neutrino.core.web.interceptor.RestControllerAdviceHandler; import java.util.List; import java.util.stream.Collectors; @@ -51,6 +53,8 @@ public class WebContextHolder { private static volatile Integer port; private static List controllerBeanWrapperList; private static volatile InterceptorRegistry interceptorRegistry; + private static volatile ExceptionHandlerRegistry exceptionHandlerRegistry; + private static volatile RestControllerAdviceHandler adviceHandler; @Autowired private ApplicationConfig applicationConfig; @Autowired @@ -126,6 +130,7 @@ public class WebContextHolder { private void initInterceptorRegistry() { interceptorRegistry = new InterceptorRegistry(); + exceptionHandlerRegistry = new ExceptionHandlerRegistry(); List beanWrapperList = applicationBeanFactory.beanWrapperList(); if (CollectionUtil.isEmpty(beanWrapperList)) { return; @@ -134,7 +139,11 @@ public class WebContextHolder { if (CollectionUtil.isEmpty(webMvcConfigurerList)) { return; } - webMvcConfigurerList.forEach(webMvcConfigurer -> webMvcConfigurer.addInterceptors(interceptorRegistry)); + webMvcConfigurerList.forEach(webMvcConfigurer -> { + webMvcConfigurer.addInterceptors(interceptorRegistry); + webMvcConfigurer.addExceptionHandler(exceptionHandlerRegistry); + adviceHandler = webMvcConfigurer.adviceHandler(); + }); } public static List getControllerBeanWrapperList() { @@ -144,4 +153,12 @@ public class WebContextHolder { public static InterceptorRegistry getInterceptorRegistry() { return interceptorRegistry; } + + public static ExceptionHandlerRegistry getExceptionHandlerRegistry() { + return exceptionHandlerRegistry; + } + + public static RestControllerAdviceHandler getAdviceHandler() { + return adviceHandler; + } } diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/ExceptionHandlerRegistry.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/ExceptionHandlerRegistry.java new file mode 100644 index 00000000..cd9cc6b4 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/ExceptionHandlerRegistry.java @@ -0,0 +1,42 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.web.interceptor; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/29 + */ +public class ExceptionHandlerRegistry { + private final List exceptionHandlerList = new ArrayList<>(); + + public void addExceptionHandler(RestControllerExceptionHandler exceptionHandler) { + exceptionHandlerList.add(exceptionHandler); + } + + public List getExceptionHandlerList() { + return exceptionHandlerList; + } +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/RestControllerAdviceHandler.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/RestControllerAdviceHandler.java new file mode 100644 index 00000000..f608bbcc --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/RestControllerAdviceHandler.java @@ -0,0 +1,45 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.web.interceptor; + +import fun.asgc.neutrino.core.web.context.HttpRequestParser; +import io.netty.channel.ChannelHandlerContext; + +import java.lang.reflect.Method; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/29 + */ +public interface RestControllerAdviceHandler { + /** + * 返回结果处理 + * @param context + * @param requestParser + * @param route + * @param targetMethod + * @param res + * @return + */ + Object advice(ChannelHandlerContext context, HttpRequestParser requestParser, String route, Method targetMethod, Object res); +} diff --git a/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/RestControllerExceptionHandler.java b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/RestControllerExceptionHandler.java new file mode 100644 index 00000000..96b87497 --- /dev/null +++ b/neutrino-core/src/main/java/fun/asgc/neutrino/core/web/interceptor/RestControllerExceptionHandler.java @@ -0,0 +1,48 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.web.interceptor; + +import fun.asgc.neutrino.core.web.context.HttpRequestParser; +import io.netty.channel.ChannelHandlerContext; + +/** + * 异常处理 + * @author: aoshiguchen + * @date: 2022/7/29 + */ +public interface RestControllerExceptionHandler { + /** + * 是否支持处理该异常 + * @param e + * @return + */ + default boolean support(Throwable e) { + return true; + } + + /** + * 异常处理 + * @param e + * @return + */ + Object handle(ChannelHandlerContext context, HttpRequestParser requestParser, Throwable e); +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/GlobalAdviceHandler.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/GlobalAdviceHandler.java new file mode 100644 index 00000000..c16fc5a6 --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/GlobalAdviceHandler.java @@ -0,0 +1,47 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.web.test1; + +import fun.asgc.neutrino.core.web.context.HttpRequestParser; +import fun.asgc.neutrino.core.web.interceptor.RestControllerAdviceHandler; +import io.netty.channel.ChannelHandlerContext; + +import java.lang.reflect.Method; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/29 + */ +public class GlobalAdviceHandler implements RestControllerAdviceHandler { + + @Override + public Object advice(ChannelHandlerContext context, HttpRequestParser requestParser, String route, Method targetMethod, Object res) { + if (res instanceof JsonResult) { + return res; + } + return new JsonResult<>() + .setCode(0) + .setData(res); + } + +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/GlobalExceptionHandler.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/GlobalExceptionHandler.java new file mode 100644 index 00000000..76cbdb12 --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/GlobalExceptionHandler.java @@ -0,0 +1,50 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.web.test1; + +import fun.asgc.neutrino.core.web.context.HttpRequestParser; +import fun.asgc.neutrino.core.web.interceptor.RestControllerExceptionHandler; +import io.netty.channel.ChannelHandlerContext; +import org.apache.commons.lang3.exception.ExceptionUtils; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/29 + */ +public class GlobalExceptionHandler implements RestControllerExceptionHandler { + + @Override + public Object handle(ChannelHandlerContext context, HttpRequestParser requestParser, Throwable e) { + if (e instanceof UserNotLoginException) { + return new JsonResult<>() + .setCode(101) + .setMsg("账号未登录") + .setStack(ExceptionUtils.getStackTrace(e)); + } + return new JsonResult<>() + .setCode(1) + .setMsg("系统异常") + .setStack(ExceptionUtils.getStackTrace(e)); + } + +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/JsonResult.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/JsonResult.java new file mode 100644 index 00000000..4d8ee245 --- /dev/null +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/JsonResult.java @@ -0,0 +1,39 @@ +/** + * Copyright (c) 2022 aoshiguchen + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package fun.asgc.neutrino.core.web.test1; + +import lombok.Data; +import lombok.experimental.Accessors; + +/** + * + * @author: aoshiguchen + * @date: 2022/7/29 + */ +@Accessors(chain = true) +@Data +public class JsonResult { + private Integer code; + private String msg; + private T data; + private String stack; +} diff --git a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/WebConfigurerAdapter.java b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/WebConfigurerAdapter.java index 56b50776..cf2c173c 100644 --- a/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/WebConfigurerAdapter.java +++ b/neutrino-core/src/test/java/fun/asgc/neutrino/core/web/test1/WebConfigurerAdapter.java @@ -23,7 +23,9 @@ package fun.asgc.neutrino.core.web.test1; import fun.asgc.neutrino.core.annotation.Configuration; import fun.asgc.neutrino.core.web.config.WebMvcConfigurer; +import fun.asgc.neutrino.core.web.interceptor.ExceptionHandlerRegistry; import fun.asgc.neutrino.core.web.interceptor.InterceptorRegistry; +import fun.asgc.neutrino.core.web.interceptor.RestControllerAdviceHandler; /** * @@ -38,4 +40,14 @@ public class WebConfigurerAdapter implements WebMvcConfigurer { registry.addInterceptor(new BaseAuthInterceptor()) .addPathPatterns("/**").excludePathPatterns("/**/*.html", "/**/*.js", "/**/*.ico"); } + + @Override + public void addExceptionHandler(ExceptionHandlerRegistry registry) { + registry.addExceptionHandler(new GlobalExceptionHandler()); + } + + @Override + public RestControllerAdviceHandler adviceHandler() { + return new GlobalAdviceHandler(); + } }