feat: not found handle

This commit is contained in:
2021-11-14 12:22:54 +08:00
parent 110a5d9138
commit 6d8b51374a
6 changed files with 40 additions and 26 deletions

View File

@@ -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);
// }
//
// };
// }
}

View File

@@ -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<String, Object> 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));
}
}

View File

@@ -37,6 +37,6 @@ public class CustomHandlerInterceptorAdapter extends HandlerInterceptorAdapter {
"No permission: " + request.getRequestURI()
).ex(HttpServletResponse.SC_FORBIDDEN);
}
return false;
return true;
}
}

View File

@@ -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);

View File

@@ -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) {

View File

@@ -0,0 +1,2 @@
spring.mvc.throw-exception-if-no-handler-found=true
spring.resources.add-mappings=false