[英]500 Internal Server Error instead of 404 in Spring Boot
[英]Spring boot web app gives 500 internal server error instead 404 not found
當沒有給定 id 的學生時,我怎么能得到 404 錯誤?
我的 Controller:
@GetMapping("/students/{stu_id}")
private Student_Information getInfo(@PathVariable("stu_id") String stu_id){
Student_Information student = studentInformationService.getStudentById(stu_id);
if(student == null) {
throw new StudentNotFoundException("id"+stu_id);
}
return student;
}
我的異常 class:
@ResponseStatus(HttpStatus.NOT_FOUND)
public class StudentNotFoundException extends RuntimeException {
public StudentNotFoundException(String message) {
super(message);
}
}
我在 POSTMAN 中得到了什么:{“時間戳”:“2021-03-21T20:50:15.072+00:00”,“狀態”:500,“錯誤”:“內部服務器錯誤”,“消息”:“”, “路徑”:“/學生/stud231”}
來自 java 的錯誤:
java.util.NoSuchElementException: No value present
at java.util.Optional.get(Optional.java:135) ~[na:1.8.0_251]
at com.example.demo.services.StudentInformationService.getStudentById(StudentInformationService.java:17) ~[classes/:na]
at com.example.demo.controllers.AppController.getInfo(AppController.java:32) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_251]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_251]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_251]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_251]
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197) ~[spring-web-5.3.4.jar:5.3.4]
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141) ~[spring-web-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1060) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:962) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.4.jar:5.3.4]
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.4.jar:5.3.4]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:626) ~[tomcat-embed-core-9.0.43.jar:4.0.FR]
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.4.jar:5.3.4]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733) ~[tomcat-embed-core-9.0.43.jar:4.0.FR]
at ...
發生這種情況是因為: repository.findById(id)
返回的 Optional 可以返回一個empty
,所以如果你調用 get() 它會拋出java.util.NoSuchElementException
。
您可以通過以下方式解決此問題: repository.findById(id).orElseGet(null)
或repository.findById(id).orElseGet(new Student_Information())
。
4XX 錯誤代碼與客戶端錯誤有關,您不應將此作為最佳實踐返回。 您可以返回 200 Ok 並返回空響應。
因為資源可用但路徑變量的記錄不存在,所以這里返回 404 不是一個好的選擇。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.