我创建了一个非常简单的自定义错误页面。我有一个问题,每当我重新启动程序并打开localhost:8080
时,它总是会引发异常,但我不希望这样。
感谢您的帮助
Caused by: java.lang.NullPointerException: null
at com.OnlineLibrary.Controller.CustomErrorController.error(CustomErrorController.java:16) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_162]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_162]
at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_162]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
... 57 common frames omitted
这是我的自定义控制器
@Controller
public class CustomErrorController implements ErrorController {
@RequestMapping("/error")
public String error(Model model, Principal user) {
model.addAttribute("sorryMessage", "Özür Dileriz " + user.getName()+ " bir hata ile karşılaştık");
model.addAttribute("weArentWrong", "Belki sen hata yaptın belki biz. Lütfen Linke tıklayarak başlangıca geri dön.");
return "error";
}
@Override
public String getErrorPath() {
// TODO Auto-generated method stub
return null;
}
}
答案 0 :(得分:0)
我不知道这是正确的方法,但是
@Controller
public class CustomErrorController implements ErrorController {
@SuppressWarnings("finally")
@RequestMapping("/error")
public String error(Model model, Principal user) {
try {
model.addAttribute("sorryMessage", "Özür Dileriz " + user.getName() + " bir hata ile karşılaştık");
model.addAttribute("weArentWrong",
"Belki sen hata yaptın belki biz. Lütfen Linke tıklayarak başlangıca geri dön.");
} catch (NullPointerException a) {
} finally {
return "error";
}
}
@Override
public String getErrorPath() {
// TODO Auto-generated method stub
return null;
}
}
通过这种方式,我现在没有出错,但是这似乎是错误的方式。