feat: not found handle
This commit is contained in:
@@ -7,22 +7,8 @@ import org.springframework.context.annotation.ComponentScan;
|
|||||||
@ComponentScan(basePackages = "me.hatter.sample")
|
@ComponentScan(basePackages = "me.hatter.sample")
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class Application {
|
public class Application {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(Application.class, 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);
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
package me.hatter.sample.common.handler;
|
package me.hatter.sample.common.handler;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import me.hatter.sample.common.message.ErrorMessage;
|
||||||
import me.hatter.sample.common.message.ErrorMessageException;
|
import me.hatter.sample.common.message.ErrorMessageException;
|
||||||
import me.hatter.tools.commons.exception.ExceptionUtil;
|
import me.hatter.tools.commons.exception.ExceptionUtil;
|
||||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
|
|
||||||
|
// @see org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
public class GlobalControllerExceptionHandler {
|
public class GlobalControllerExceptionHandler {
|
||||||
|
|
||||||
@@ -19,14 +20,31 @@ public class GlobalControllerExceptionHandler {
|
|||||||
// @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR, reason = "internal error")
|
// @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR, reason = "internal error")
|
||||||
@ExceptionHandler(Throwable.class)
|
@ExceptionHandler(Throwable.class)
|
||||||
public void handleException(HttpServletResponse response, Throwable t) throws IOException {
|
public void handleException(HttpServletResponse response, Throwable t) throws IOException {
|
||||||
|
System.out.println("[GlobalControllerExceptionHandler] " + t.getClass());
|
||||||
if (t instanceof ErrorMessageException) {
|
if (t instanceof ErrorMessageException) {
|
||||||
response.setStatus(((ErrorMessageException) t).getStatusCode());
|
processErrorMessage(response,
|
||||||
response.getWriter().write(JSON.toJSONString(((ErrorMessageException) t).getErrorMessage(), true));
|
((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 {
|
} else {
|
||||||
final Map<String, Object> result = new LinkedHashMap<>();
|
final ErrorMessage errorMessage = new ErrorMessage("internal_error",
|
||||||
result.put("stacktrace", ExceptionUtil.printStackTrace(t));
|
ExceptionUtil.printStackTrace(t)
|
||||||
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
|
);
|
||||||
response.getWriter().write(JSON.toJSONString(result, true));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -37,6 +37,6 @@ public class CustomHandlerInterceptorAdapter extends HandlerInterceptorAdapter {
|
|||||||
"No permission: " + request.getRequestURI()
|
"No permission: " + request.getRequestURI()
|
||||||
).ex(HttpServletResponse.SC_FORBIDDEN);
|
).ex(HttpServletResponse.SC_FORBIDDEN);
|
||||||
}
|
}
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import org.springframework.web.util.WebUtils;
|
|||||||
|
|
||||||
import javax.servlet.http.Cookie;
|
import javax.servlet.http.Cookie;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class CustomSessionArgumentResolver implements HandlerMethodArgumentResolver {
|
public class CustomSessionArgumentResolver implements HandlerMethodArgumentResolver {
|
||||||
@@ -28,6 +29,7 @@ public class CustomSessionArgumentResolver implements HandlerMethodArgumentResol
|
|||||||
Cookie cookie = WebUtils.getCookie(httpServletRequest, "hatter_session_id");
|
Cookie cookie = WebUtils.getCookie(httpServletRequest, "hatter_session_id");
|
||||||
if (cookie == null) {
|
if (cookie == null) {
|
||||||
cookie = new Cookie("hatter_session_id", RandomTool.secureRandom().nextBytes(64).asBase58());
|
cookie = new Cookie("hatter_session_id", RandomTool.secureRandom().nextBytes(64).asBase58());
|
||||||
|
cookie.setMaxAge((int) TimeUnit.DAYS.toSeconds(30));
|
||||||
cookie.setPath("/");
|
cookie.setPath("/");
|
||||||
cookie.setHttpOnly(true);
|
cookie.setHttpOnly(true);
|
||||||
httpServletResponse.addCookie(cookie);
|
httpServletResponse.addCookie(cookie);
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import me.hatter.sample.common.annotation.Permission;
|
|||||||
import me.hatter.sample.common.annotation.SystemTime;
|
import me.hatter.sample.common.annotation.SystemTime;
|
||||||
import me.hatter.sample.common.session.CustomSession;
|
import me.hatter.sample.common.session.CustomSession;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
@@ -13,8 +14,7 @@ public class SampleController {
|
|||||||
|
|
||||||
@Permission
|
@Permission
|
||||||
@GetMapping("/")
|
@GetMapping("/")
|
||||||
public String home(
|
public String home(@SystemTime Date time) {
|
||||||
@SystemTime Date time) {
|
|
||||||
return "Hello, World: " + time;
|
return "Hello, World: " + time;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,6 +29,12 @@ public class SampleController {
|
|||||||
return "Hello, World!";
|
return "Hello, World!";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Permission
|
||||||
|
@GetMapping("/id")
|
||||||
|
public String showId(@RequestParam Integer id) {
|
||||||
|
return "Hello: " + id;
|
||||||
|
}
|
||||||
|
|
||||||
@Permission
|
@Permission
|
||||||
@GetMapping("/count")
|
@GetMapping("/count")
|
||||||
public String count(CustomSession session) {
|
public String count(CustomSession session) {
|
||||||
|
|||||||
2
src/main/resources/application.properties
Normal file
2
src/main/resources/application.properties
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
spring.mvc.throw-exception-if-no-handler-found=true
|
||||||
|
spring.resources.add-mappings=false
|
||||||
Reference in New Issue
Block a user