From 6d8b51374abf9231b1aaab511c35a07a5eea3356 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 14 Nov 2021 12:22:54 +0800 Subject: [PATCH] feat: not found handle --- .../java/me/hatter/sample/Application.java | 16 +-------- .../GlobalControllerExceptionHandler.java | 34 ++++++++++++++----- .../CustomHandlerInterceptorAdapter.java | 2 +- .../CustomSessionArgumentResolver.java | 2 ++ .../web/controller/SampleController.java | 10 ++++-- src/main/resources/application.properties | 2 ++ 6 files changed, 40 insertions(+), 26 deletions(-) create mode 100644 src/main/resources/application.properties diff --git a/src/main/java/me/hatter/sample/Application.java b/src/main/java/me/hatter/sample/Application.java index 1683587..38e694c 100644 --- a/src/main/java/me/hatter/sample/Application.java +++ b/src/main/java/me/hatter/sample/Application.java @@ -7,22 +7,8 @@ import org.springframework.context.annotation.ComponentScan; @ComponentScan(basePackages = "me.hatter.sample") @SpringBootApplication public class Application { + public static void main(String[] args) { SpringApplication.run(Application.class, args); } - -// @Bean -// public CommandLineRunner commandLineRunner(ApplicationContext ctx) { -// return args -> { -// -// System.out.println("Let's inspect the beans provided by Spring Boot:"); -// -// String[] beanNames = ctx.getBeanDefinitionNames(); -// Arrays.sort(beanNames); -// for (String beanName : beanNames) { -// System.out.println(beanName); -// } -// -// }; -// } } diff --git a/src/main/java/me/hatter/sample/common/handler/GlobalControllerExceptionHandler.java b/src/main/java/me/hatter/sample/common/handler/GlobalControllerExceptionHandler.java index 4cf820c..314d933 100644 --- a/src/main/java/me/hatter/sample/common/handler/GlobalControllerExceptionHandler.java +++ b/src/main/java/me/hatter/sample/common/handler/GlobalControllerExceptionHandler.java @@ -1,17 +1,18 @@ package me.hatter.sample.common.handler; import com.alibaba.fastjson.JSON; +import me.hatter.sample.common.message.ErrorMessage; import me.hatter.sample.common.message.ErrorMessageException; import me.hatter.tools.commons.exception.ExceptionUtil; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.servlet.NoHandlerFoundException; import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import java.util.LinkedHashMap; -import java.util.Map; +// @see org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler @ControllerAdvice public class GlobalControllerExceptionHandler { @@ -19,14 +20,31 @@ public class GlobalControllerExceptionHandler { // @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR, reason = "internal error") @ExceptionHandler(Throwable.class) public void handleException(HttpServletResponse response, Throwable t) throws IOException { + System.out.println("[GlobalControllerExceptionHandler] " + t.getClass()); if (t instanceof ErrorMessageException) { - response.setStatus(((ErrorMessageException) t).getStatusCode()); - response.getWriter().write(JSON.toJSONString(((ErrorMessageException) t).getErrorMessage(), true)); + processErrorMessage(response, + ((ErrorMessageException) t).getStatusCode(), + ((ErrorMessageException) t).getErrorMessage() + ); + } else if (t instanceof NoHandlerFoundException) { + final ErrorMessage errorMessage = new ErrorMessage("resource_not_found", + "Resource not found: " + + ((NoHandlerFoundException) t).getHttpMethod() + + " " + ((NoHandlerFoundException) t).getRequestURL() + ); + processErrorMessage(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorMessage); } else { - final Map result = new LinkedHashMap<>(); - result.put("stacktrace", ExceptionUtil.printStackTrace(t)); - response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); - response.getWriter().write(JSON.toJSONString(result, true)); + final ErrorMessage errorMessage = new ErrorMessage("internal_error", + ExceptionUtil.printStackTrace(t) + ); + processErrorMessage(response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, errorMessage); } } + + private void processErrorMessage(HttpServletResponse response, int statusCode, ErrorMessage errorMessage) + throws IOException { + response.setStatus(statusCode); + response.setContentType("application/json;charset=utf-8"); + response.getWriter().write(JSON.toJSONString(errorMessage, true)); + } } diff --git a/src/main/java/me/hatter/sample/common/interceptor/CustomHandlerInterceptorAdapter.java b/src/main/java/me/hatter/sample/common/interceptor/CustomHandlerInterceptorAdapter.java index d5e422e..52bd17e 100644 --- a/src/main/java/me/hatter/sample/common/interceptor/CustomHandlerInterceptorAdapter.java +++ b/src/main/java/me/hatter/sample/common/interceptor/CustomHandlerInterceptorAdapter.java @@ -37,6 +37,6 @@ public class CustomHandlerInterceptorAdapter extends HandlerInterceptorAdapter { "No permission: " + request.getRequestURI() ).ex(HttpServletResponse.SC_FORBIDDEN); } - return false; + return true; } } diff --git a/src/main/java/me/hatter/sample/common/resolver/CustomSessionArgumentResolver.java b/src/main/java/me/hatter/sample/common/resolver/CustomSessionArgumentResolver.java index 90b2275..27fdb6e 100644 --- a/src/main/java/me/hatter/sample/common/resolver/CustomSessionArgumentResolver.java +++ b/src/main/java/me/hatter/sample/common/resolver/CustomSessionArgumentResolver.java @@ -13,6 +13,7 @@ import org.springframework.web.util.WebUtils; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; +import java.util.concurrent.TimeUnit; @Component public class CustomSessionArgumentResolver implements HandlerMethodArgumentResolver { @@ -28,6 +29,7 @@ public class CustomSessionArgumentResolver implements HandlerMethodArgumentResol Cookie cookie = WebUtils.getCookie(httpServletRequest, "hatter_session_id"); if (cookie == null) { cookie = new Cookie("hatter_session_id", RandomTool.secureRandom().nextBytes(64).asBase58()); + cookie.setMaxAge((int) TimeUnit.DAYS.toSeconds(30)); cookie.setPath("/"); cookie.setHttpOnly(true); httpServletResponse.addCookie(cookie); diff --git a/src/main/java/me/hatter/sample/web/controller/SampleController.java b/src/main/java/me/hatter/sample/web/controller/SampleController.java index d744999..801ad36 100644 --- a/src/main/java/me/hatter/sample/web/controller/SampleController.java +++ b/src/main/java/me/hatter/sample/web/controller/SampleController.java @@ -4,6 +4,7 @@ import me.hatter.sample.common.annotation.Permission; import me.hatter.sample.common.annotation.SystemTime; import me.hatter.sample.common.session.CustomSession; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @@ -13,8 +14,7 @@ public class SampleController { @Permission @GetMapping("/") - public String home( - @SystemTime Date time) { + public String home(@SystemTime Date time) { return "Hello, World: " + time; } @@ -29,6 +29,12 @@ public class SampleController { return "Hello, World!"; } + @Permission + @GetMapping("/id") + public String showId(@RequestParam Integer id) { + return "Hello: " + id; + } + @Permission @GetMapping("/count") public String count(CustomSession session) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..271eb89 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.mvc.throw-exception-if-no-handler-found=true +spring.resources.add-mappings=false