diff --git a/cicada-core/pom.xml b/cicada-core/pom.xml
index 4309602..aa4f9ba 100644
--- a/cicada-core/pom.xml
+++ b/cicada-core/pom.xml
@@ -10,7 +10,7 @@
4.0.0
cicada-core
- 2.0.2
+ 2.0.3
jar
cicada-core
diff --git a/cicada-core/src/main/java/top/crossoverjie/cicada/server/annotation/CicadaCustomizeExceptionHandle.java b/cicada-core/src/main/java/top/crossoverjie/cicada/server/annotation/CicadaCustomizeExceptionHandle.java
new file mode 100644
index 0000000..947961b
--- /dev/null
+++ b/cicada-core/src/main/java/top/crossoverjie/cicada/server/annotation/CicadaCustomizeExceptionHandle.java
@@ -0,0 +1,24 @@
+package top.crossoverjie.cicada.server.annotation;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Function:
+ * annotation of cicada exception
+ * @author hugui
+ * Date: 2019-11-17 12:07
+ * @since JDK 1.8
+ */
+
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface CicadaCustomizeExceptionHandle {
+
+ Class> value();
+
+}
diff --git a/cicada-core/src/main/java/top/crossoverjie/cicada/server/exception/CustomizeHandleException.java b/cicada-core/src/main/java/top/crossoverjie/cicada/server/exception/CustomizeHandleException.java
new file mode 100644
index 0000000..8d649ac
--- /dev/null
+++ b/cicada-core/src/main/java/top/crossoverjie/cicada/server/exception/CustomizeHandleException.java
@@ -0,0 +1,22 @@
+package top.crossoverjie.cicada.server.exception;
+
+import top.crossoverjie.cicada.server.context.CicadaContext;
+
+/**
+ * Function: global exception handle
+ *
+ * @author hugui
+ * Date: 2019-11-17 17:12
+ * @since JDK 1.8
+ */
+
+public interface CustomizeHandleException {
+
+ /**
+ * exception handle
+ * @param context
+ * @param e
+ */
+ void resolveException(CicadaContext context,Exception e) ;
+
+}
diff --git a/cicada-core/src/main/java/top/crossoverjie/cicada/server/handle/HttpDispatcher.java b/cicada-core/src/main/java/top/crossoverjie/cicada/server/handle/HttpDispatcher.java
index 3a72a4d..67d1efd 100644
--- a/cicada-core/src/main/java/top/crossoverjie/cicada/server/handle/HttpDispatcher.java
+++ b/cicada-core/src/main/java/top/crossoverjie/cicada/server/handle/HttpDispatcher.java
@@ -1,13 +1,27 @@
package top.crossoverjie.cicada.server.handle;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Map;
+
+import org.slf4j.Logger;
+
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
-import io.netty.handler.codec.http.*;
+import io.netty.handler.codec.http.DefaultFullHttpResponse;
+import io.netty.handler.codec.http.DefaultHttpRequest;
+import io.netty.handler.codec.http.HttpHeaderNames;
+import io.netty.handler.codec.http.HttpHeaders;
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.netty.handler.codec.http.HttpVersion;
+import io.netty.handler.codec.http.QueryStringDecoder;
import io.netty.handler.codec.http.cookie.Cookie;
-import org.slf4j.Logger;
import top.crossoverjie.cicada.base.log.LoggerBuilder;
import top.crossoverjie.cicada.server.action.param.Param;
import top.crossoverjie.cicada.server.action.param.ParamMap;
@@ -15,163 +29,177 @@
import top.crossoverjie.cicada.server.action.req.CicadaRequest;
import top.crossoverjie.cicada.server.action.res.CicadaHttpResponse;
import top.crossoverjie.cicada.server.action.res.CicadaResponse;
+import top.crossoverjie.cicada.server.annotation.CicadaCustomizeExceptionHandle;
import top.crossoverjie.cicada.server.bean.CicadaBeanManager;
import top.crossoverjie.cicada.server.config.AppConfig;
import top.crossoverjie.cicada.server.constant.CicadaConstant;
import top.crossoverjie.cicada.server.context.CicadaContext;
import top.crossoverjie.cicada.server.exception.CicadaException;
+import top.crossoverjie.cicada.server.exception.CustomizeHandleException;
import top.crossoverjie.cicada.server.exception.GlobalHandelException;
import top.crossoverjie.cicada.server.intercept.InterceptProcess;
import top.crossoverjie.cicada.server.route.RouteProcess;
import top.crossoverjie.cicada.server.route.RouterScanner;
-import java.lang.reflect.Method;
-import java.net.URLDecoder;
-import java.nio.charset.StandardCharsets;
-import java.util.List;
-import java.util.Map;
-
/**
* Function:
*
- * @author crossoverJie
- * Date: 2018/8/30 18:47
+ * @author crossoverJie Date: 2018/8/30 18:47
* @since JDK 1.8
*/
@ChannelHandler.Sharable
public final class HttpDispatcher extends SimpleChannelInboundHandler {
- private static final Logger LOGGER = LoggerBuilder.getLogger(HttpDispatcher.class);
-
- private final AppConfig appConfig = AppConfig.getInstance();
- private final InterceptProcess interceptProcess = InterceptProcess.getInstance();
- private final RouterScanner routerScanner = RouterScanner.getInstance();
- private final RouteProcess routeProcess = RouteProcess.getInstance() ;
- private final CicadaBeanManager cicadaBeanManager = CicadaBeanManager.getInstance() ;
- private final GlobalHandelException exceptionHandle = cicadaBeanManager.exceptionHandle() ;
- private Exception exception ;
-
- @Override
- public void channelRead0(ChannelHandlerContext ctx, DefaultHttpRequest httpRequest) {
-
- CicadaRequest cicadaRequest = CicadaHttpRequest.init(httpRequest);
- CicadaResponse cicadaResponse = CicadaHttpResponse.init();
-
- // set current thread request and response
- CicadaContext.setContext(new CicadaContext(cicadaRequest, cicadaResponse));
-
- try {
- // request uri
- String uri = cicadaRequest.getUrl();
- QueryStringDecoder queryStringDecoder = new QueryStringDecoder(URLDecoder.decode(httpRequest.uri(), "utf-8"));
-
- // check Root Path
- appConfig.checkRootPath(uri, queryStringDecoder);
-
- // route Action
- //Class> actionClazz = routeAction(queryStringDecoder, appConfig);
-
- //build paramMap
- Param paramMap = buildParamMap(queryStringDecoder);
-
- //load interceptors
- interceptProcess.loadInterceptors();
-
- //interceptor before
- boolean access = interceptProcess.processBefore(paramMap);
- if (!access) {
- return;
- }
-
- // execute Method
- Method method = routerScanner.routeMethod(queryStringDecoder);
- routeProcess.invoke(method,queryStringDecoder) ;
-
-
- //WorkAction action = (WorkAction) actionClazz.newInstance();
- //action.execute(CicadaContext.getContext(), paramMap);
-
-
- // interceptor after
- interceptProcess.processAfter(paramMap);
-
- } catch (Exception e) {
- exceptionCaught(ctx, e);
- } finally {
- // Response
- responseContent(ctx);
-
- // remove cicada thread context
- CicadaContext.removeContext();
- }
-
-
- }
-
-
- /**
- * Response
- *
- * @param ctx
- */
- private void responseContent(ChannelHandlerContext ctx) {
- CicadaResponse cicadaResponse = CicadaContext.getResponse();
- String context = cicadaResponse.getHttpContent() ;
-
- ByteBuf buf = Unpooled.wrappedBuffer(context.getBytes(StandardCharsets.UTF_8));
- DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, buf);
- buildHeader(response);
- ctx.writeAndFlush(response);
- }
-
- /**
- * build paramMap
- *
- * @param queryStringDecoder
- * @return
- */
- private Param buildParamMap(QueryStringDecoder queryStringDecoder) {
- Map> parameters = queryStringDecoder.parameters();
- Param paramMap = new ParamMap();
- for (Map.Entry> stringListEntry : parameters.entrySet()) {
- String key = stringListEntry.getKey();
- List value = stringListEntry.getValue();
- paramMap.put(key, value.get(0));
- }
- return paramMap;
- }
-
-
-
-
- @Override
- public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
- exception = (Exception) cause;
- if (CicadaException.isResetByPeer(cause.getMessage())){
- return;
- }
-
- if (exceptionHandle != null){
- exceptionHandle.resolveException(CicadaContext.getContext(),exception);
- }
- }
-
- /**
- * build Header
- *
- * @param response
- */
- private void buildHeader(DefaultFullHttpResponse response) {
- CicadaResponse cicadaResponse = CicadaContext.getResponse();
-
- HttpHeaders headers = response.headers();
- headers.setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
- headers.set(HttpHeaderNames.CONTENT_TYPE, cicadaResponse.getContentType());
-
- List cookies = cicadaResponse.cookies();
- for (Cookie cookie : cookies) {
- headers.add(CicadaConstant.ContentType.SET_COOKIE, io.netty.handler.codec.http.cookie.ServerCookieEncoder.LAX.encode(cookie));
- }
-
- }
+ private static final Logger LOGGER = LoggerBuilder.getLogger(HttpDispatcher.class);
+
+ private final AppConfig appConfig = AppConfig.getInstance();
+ private final InterceptProcess interceptProcess = InterceptProcess.getInstance();
+ private final RouterScanner routerScanner = RouterScanner.getInstance();
+ private final RouteProcess routeProcess = RouteProcess.getInstance();
+ private final CicadaBeanManager cicadaBeanManager = CicadaBeanManager.getInstance();
+ private final GlobalHandelException exceptionHandle = cicadaBeanManager.exceptionHandle();
+ private Exception exception;
+
+ @Override
+ public void channelRead0(ChannelHandlerContext ctx, DefaultHttpRequest httpRequest) {
+
+ CicadaRequest cicadaRequest = CicadaHttpRequest.init(httpRequest);
+ CicadaResponse cicadaResponse = CicadaHttpResponse.init();
+
+ // set current thread request and response
+ CicadaContext.setContext(new CicadaContext(cicadaRequest, cicadaResponse));
+
+ try {
+ // request uri
+ String uri = cicadaRequest.getUrl();
+ QueryStringDecoder queryStringDecoder = new QueryStringDecoder(
+ URLDecoder.decode(httpRequest.uri(), "utf-8"));
+
+ // check Root Path
+ appConfig.checkRootPath(uri, queryStringDecoder);
+
+ // route Action
+ // Class> actionClazz = routeAction(queryStringDecoder, appConfig);
+
+ // build paramMap
+ Param paramMap = buildParamMap(queryStringDecoder);
+
+ // load interceptors
+ interceptProcess.loadInterceptors();
+
+ // interceptor before
+ boolean access = interceptProcess.processBefore(paramMap);
+ if (!access) {
+ return;
+ }
+
+ // execute Method
+ Method method = routerScanner.routeMethod(queryStringDecoder);
+ routeProcess.invoke(method, queryStringDecoder);
+
+ // WorkAction action = (WorkAction) actionClazz.newInstance();
+ // action.execute(CicadaContext.getContext(), paramMap);
+
+ // interceptor after
+ interceptProcess.processAfter(paramMap);
+
+ } catch (Exception e) {
+ exceptionCaught(ctx, e);
+ } finally {
+ // Response
+ responseContent(ctx);
+
+ // remove cicada thread context
+ CicadaContext.removeContext();
+ }
+
+ }
+
+ /**
+ * Response
+ *
+ * @param ctx
+ */
+ private void responseContent(ChannelHandlerContext ctx) {
+ CicadaResponse cicadaResponse = CicadaContext.getResponse();
+ String context = cicadaResponse.getHttpContent();
+
+ ByteBuf buf = Unpooled.wrappedBuffer(context.getBytes(StandardCharsets.UTF_8));
+ DefaultFullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK,
+ buf);
+ buildHeader(response);
+ ctx.writeAndFlush(response);
+ }
+
+ /**
+ * build paramMap
+ *
+ * @param queryStringDecoder
+ * @return
+ */
+ private Param buildParamMap(QueryStringDecoder queryStringDecoder) {
+ Map> parameters = queryStringDecoder.parameters();
+ Param paramMap = new ParamMap();
+ for (Map.Entry> stringListEntry : parameters.entrySet()) {
+ String key = stringListEntry.getKey();
+ List value = stringListEntry.getValue();
+ paramMap.put(key, value.get(0));
+ }
+ return paramMap;
+ }
+
+ @Override
+ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
+ exception = (Exception) cause;
+ if (CicadaException.isResetByPeer(cause.getMessage())) {
+ return;
+ }
+
+ try {
+ Class> actionClazz = routerScanner.routeClass(CicadaContext.getContext().request().getUrl());
+ if (actionClazz != null) {
+ Annotation annotation = actionClazz.getAnnotation(CicadaCustomizeExceptionHandle.class);
+ CicadaCustomizeExceptionHandle cceHandle = null;
+ if (annotation != null) {
+ cceHandle = (CicadaCustomizeExceptionHandle) annotation;
+ }
+
+ if (cceHandle != null) {
+ Object bean = cicadaBeanManager.getBean(cceHandle.value());
+ if (bean != null) {
+ CustomizeHandleException customizeHandleException = (CustomizeHandleException) bean;
+ customizeHandleException.resolveException(CicadaContext.getContext(), exception);
+ }
+ return;
+ }
+ }
+
+ if (exceptionHandle != null) {
+ exceptionHandle.resolveException(CicadaContext.getContext(), exception);
+ }
+ } catch (Exception e) {
+ LOGGER.error(String.format("can't found refer class : %s", CicadaContext.getContext().request().getUrl()));
+ }
+
+ }
+
+ /**
+ * build Header
+ *
+ * @param response
+ */
+ private void buildHeader(DefaultFullHttpResponse response) {
+ CicadaResponse cicadaResponse = CicadaContext.getResponse();
+
+ HttpHeaders headers = response.headers();
+ headers.setInt(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
+ headers.set(HttpHeaderNames.CONTENT_TYPE, cicadaResponse.getContentType());
+
+ List cookies = cicadaResponse.cookies();
+ for (Cookie cookie : cookies) {
+ headers.add(CicadaConstant.ContentType.SET_COOKIE,
+ io.netty.handler.codec.http.cookie.ServerCookieEncoder.LAX.encode(cookie));
+ }
+
+ }
}
diff --git a/cicada-core/src/main/java/top/crossoverjie/cicada/server/route/RouterScanner.java b/cicada-core/src/main/java/top/crossoverjie/cicada/server/route/RouterScanner.java
index 550c187..0d19c7d 100644
--- a/cicada-core/src/main/java/top/crossoverjie/cicada/server/route/RouterScanner.java
+++ b/cicada-core/src/main/java/top/crossoverjie/cicada/server/route/RouterScanner.java
@@ -1,5 +1,12 @@
package top.crossoverjie.cicada.server.route;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import javax.annotation.PostConstruct;
+
import io.netty.handler.codec.http.QueryStringDecoder;
import top.crossoverjie.cicada.server.annotation.CicadaAction;
import top.crossoverjie.cicada.server.annotation.CicadaRoute;
@@ -9,11 +16,6 @@
import top.crossoverjie.cicada.server.exception.CicadaException;
import top.crossoverjie.cicada.server.reflect.ClassScanner;
-import java.lang.reflect.Method;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Set;
-
/**
* Function:
*
@@ -23,7 +25,8 @@
*/
public class RouterScanner {
- private static Map routes = null;
+ private static Map methodRoutes = null;
+ private static Map> classRoutes = null;
private volatile static RouterScanner routerScanner;
@@ -48,6 +51,15 @@ public static RouterScanner getInstance() {
private RouterScanner() {
}
+ @PostConstruct
+ public void routeMapping() throws Exception {
+ if (classRoutes == null || methodRoutes == null) {
+ methodRoutes = new HashMap<>(16);
+ classRoutes = new HashMap<>(16);
+ loadRouteMapping(appConfig.getRootPackageName());
+ }
+ }
+
/**
* get route method
*
@@ -56,9 +68,8 @@ private RouterScanner() {
* @throws Exception
*/
public Method routeMethod(QueryStringDecoder queryStringDecoder) throws Exception {
- if (routes == null) {
- routes = new HashMap<>(16);
- loadRouteMethods(appConfig.getRootPackageName());
+ if (methodRoutes == null) {
+ routeMapping();
}
//default response
@@ -68,15 +79,45 @@ public Method routeMethod(QueryStringDecoder queryStringDecoder) throws Exceptio
return null;
}
- Method method = routes.get(queryStringDecoder.path());
+ Method method = methodRoutes.get(queryStringDecoder.path());
if (method == null) {
throw new CicadaException(StatusEnum.NOT_FOUND);
}
return method;
+ }
+
+ /**
+ * get route class
+ *
+ * @param queryStringDecoder
+ * @return
+ * @throws Exception
+ */
+ public Class> routeClass(String url) throws Exception {
+ if (classRoutes == null) {
+ routeMapping();
+ }
+
+ //default response
+ boolean defaultResponse = defaultResponse(url);
+ if (defaultResponse) {
+ return null;
+ }
+
+ String[] routePaths = url.split("/");
+ if(routePaths == null || routePaths.length < 3) {
+ return null;
+ }
+ Class> clazz = classRoutes.get("/"+routePaths[1]+"/"+routePaths[2]);
+ if (clazz == null) {
+ throw new CicadaException(StatusEnum.NOT_FOUND);
+ }
+
+ return clazz;
}
private boolean defaultResponse(String path) {
@@ -87,22 +128,32 @@ private boolean defaultResponse(String path) {
}
return false;
}
+
-
- private void loadRouteMethods(String packageName) throws Exception {
- Set> classes = ClassScanner.getClasses(packageName);
+ /**
+ * load class & method mapping
+ * @param packageName
+ * @throws Exception
+ */
+ public void loadRouteMapping(String packageName) throws Exception {
+ Set> classes = ClassScanner.getClasses(packageName);
for (Class> aClass : classes) {
+ CicadaAction cicadaAction = aClass.getAnnotation(CicadaAction.class);
+ if(cicadaAction == null) {
+ continue;
+ }
+
+ classRoutes.put(appConfig.getRootPath() + "/" + cicadaAction.value(), aClass);
+
Method[] declaredMethods = aClass.getMethods();
-
for (Method method : declaredMethods) {
- CicadaRoute annotation = method.getAnnotation(CicadaRoute.class);
- if (annotation == null) {
+ CicadaRoute routeAnnotation = method.getAnnotation(CicadaRoute.class);
+ if (routeAnnotation == null) {
continue;
}
- CicadaAction cicadaAction = aClass.getAnnotation(CicadaAction.class);
- routes.put(appConfig.getRootPath() + "/" + cicadaAction.value() + "/" + annotation.value(), method);
+ methodRoutes.put(appConfig.getRootPath() + "/" + cicadaAction.value() + "/" + routeAnnotation.value(), method);
}
}
}
diff --git a/cicada-example/pom.xml b/cicada-example/pom.xml
index 6c5b6e5..583dc0b 100644
--- a/cicada-example/pom.xml
+++ b/cicada-example/pom.xml
@@ -18,7 +18,7 @@
top.crossoverjie.opensource
cicada-core
- 2.0.2
+ 2.0.3
diff --git a/cicada-example/src/main/java/top/crossoverjie/cicada/example/action/RouteAction.java b/cicada-example/src/main/java/top/crossoverjie/cicada/example/action/RouteAction.java
index fec0557..3e84edd 100644
--- a/cicada-example/src/main/java/top/crossoverjie/cicada/example/action/RouteAction.java
+++ b/cicada-example/src/main/java/top/crossoverjie/cicada/example/action/RouteAction.java
@@ -1,12 +1,15 @@
package top.crossoverjie.cicada.example.action;
import org.slf4j.Logger;
+
import top.crossoverjie.cicada.base.log.LoggerBuilder;
import top.crossoverjie.cicada.example.exception.ExceptionHandle;
+import top.crossoverjie.cicada.example.exception.RouteExceptionHandle;
import top.crossoverjie.cicada.example.req.DemoReq;
import top.crossoverjie.cicada.server.action.req.Cookie;
import top.crossoverjie.cicada.server.action.res.WorkRes;
import top.crossoverjie.cicada.server.annotation.CicadaAction;
+import top.crossoverjie.cicada.server.annotation.CicadaCustomizeExceptionHandle;
import top.crossoverjie.cicada.server.annotation.CicadaRoute;
import top.crossoverjie.cicada.server.bean.CicadaBeanManager;
import top.crossoverjie.cicada.server.context.CicadaContext;
@@ -19,6 +22,7 @@
* @since JDK 1.8
*/
@CicadaAction("routeAction")
+@CicadaCustomizeExceptionHandle(RouteExceptionHandle.class)
public class RouteAction {
private static final Logger LOGGER = LoggerBuilder.getLogger(RouteAction.class);
diff --git a/cicada-example/src/main/java/top/crossoverjie/cicada/example/action/TextAction.java b/cicada-example/src/main/java/top/crossoverjie/cicada/example/action/TextAction.java
index 36d9c0b..c29e136 100644
--- a/cicada-example/src/main/java/top/crossoverjie/cicada/example/action/TextAction.java
+++ b/cicada-example/src/main/java/top/crossoverjie/cicada/example/action/TextAction.java
@@ -1,6 +1,8 @@
package top.crossoverjie.cicada.example.action;
+import top.crossoverjie.cicada.example.exception.TextExceptionHandle;
import top.crossoverjie.cicada.server.annotation.CicadaAction;
+import top.crossoverjie.cicada.server.annotation.CicadaCustomizeExceptionHandle;
import top.crossoverjie.cicada.server.annotation.CicadaRoute;
import top.crossoverjie.cicada.server.context.CicadaContext;
@@ -12,6 +14,7 @@
* @since JDK 1.8
*/
@CicadaAction("textAction")
+@CicadaCustomizeExceptionHandle(TextExceptionHandle.class)
public class TextAction {
diff --git a/cicada-example/src/main/java/top/crossoverjie/cicada/example/exception/ExceptionHandle.java b/cicada-example/src/main/java/top/crossoverjie/cicada/example/exception/ExceptionHandle.java
index 70e2d3e..22d05ca 100644
--- a/cicada-example/src/main/java/top/crossoverjie/cicada/example/exception/ExceptionHandle.java
+++ b/cicada-example/src/main/java/top/crossoverjie/cicada/example/exception/ExceptionHandle.java
@@ -15,7 +15,7 @@
* @since JDK 1.8
*/
-@CicadaBean
+@CicadaBean("ExceptionHandle")
public class ExceptionHandle implements GlobalHandelException {
private final static Logger LOGGER = LoggerBuilder.getLogger(ExceptionHandle.class);
diff --git a/cicada-example/src/main/java/top/crossoverjie/cicada/example/exception/RouteExceptionHandle.java b/cicada-example/src/main/java/top/crossoverjie/cicada/example/exception/RouteExceptionHandle.java
new file mode 100644
index 0000000..8075fb2
--- /dev/null
+++ b/cicada-example/src/main/java/top/crossoverjie/cicada/example/exception/RouteExceptionHandle.java
@@ -0,0 +1,33 @@
+package top.crossoverjie.cicada.example.exception;
+
+import org.slf4j.Logger;
+
+import top.crossoverjie.cicada.base.log.LoggerBuilder;
+import top.crossoverjie.cicada.server.action.res.WorkRes;
+import top.crossoverjie.cicada.server.annotation.CicadaBean;
+import top.crossoverjie.cicada.server.context.CicadaContext;
+import top.crossoverjie.cicada.server.exception.CustomizeHandleException;
+
+/**
+ * Function:
+ * customize route exception
+ * @author hugui
+ * Date: 2019-11-17 12:07
+ * @since JDK 1.8
+ */
+
+@CicadaBean("RouteExceptionHandle")
+public class RouteExceptionHandle implements CustomizeHandleException {
+ private static final Logger LOGGER = LoggerBuilder.getLogger(RouteExceptionHandle.class);
+
+ @Override
+ public void resolveException(CicadaContext context, Exception e) {
+
+ // handle your customize error
+ LOGGER.error("Exception", e);
+ WorkRes workRes = new WorkRes();
+ workRes.setCode("500");
+ workRes.setMessage("Route Error : " + e.getClass().getName() + "系统运行出现异常");
+ context.json(workRes);
+ }
+}
diff --git a/cicada-example/src/main/java/top/crossoverjie/cicada/example/exception/TextExceptionHandle.java b/cicada-example/src/main/java/top/crossoverjie/cicada/example/exception/TextExceptionHandle.java
new file mode 100644
index 0000000..74d737b
--- /dev/null
+++ b/cicada-example/src/main/java/top/crossoverjie/cicada/example/exception/TextExceptionHandle.java
@@ -0,0 +1,33 @@
+package top.crossoverjie.cicada.example.exception;
+
+import org.slf4j.Logger;
+
+import top.crossoverjie.cicada.base.log.LoggerBuilder;
+import top.crossoverjie.cicada.server.action.res.WorkRes;
+import top.crossoverjie.cicada.server.annotation.CicadaBean;
+import top.crossoverjie.cicada.server.context.CicadaContext;
+import top.crossoverjie.cicada.server.exception.CustomizeHandleException;
+
+/**
+ * Function:
+ * customize route exception
+ * @author hugui
+ * Date: 2019-11-17 12:07
+ * @since JDK 1.8
+ */
+
+@CicadaBean("TextExceptionHandle")
+public class TextExceptionHandle implements CustomizeHandleException {
+ private static final Logger LOGGER = LoggerBuilder.getLogger(TextExceptionHandle.class);
+
+ @Override
+ public void resolveException(CicadaContext context, Exception e) {
+
+ // handle your customize error
+ LOGGER.error("Exception", e);
+ WorkRes workRes = new WorkRes();
+ workRes.setCode("500");
+ workRes.setMessage("Text Error : " + e.getClass().getName() + "系统运行出现异常");
+ context.json(workRes);
+ }
+}