Spring 启动 Web 应用程序给出 500 内部服务器错误而不是 404 未找到

Spring boot web app gives 500 internal server error instead 404 not found

当没有给定 id 的学生时,我如何得到 404 错误?

我的控制器:

    @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, "error": "内部服务器错误", “信息”: ””, “路径”:“/students/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 ...

发生这种情况是因为可选 return编辑者: repository.findById(id) 可以 return 一个 empty,所以如果你调用 get() 它会抛出一个 java.util.NoSuchElementException.

您可以通过以下方式解决此问题: repository.findById(id).orElseGet(null) 要么 repository.findById(id).orElseGet(new Student_Information()).

4XX 错误代码与客户端错误有关,您不应 return 将此作为最佳做法。您可以 return 200 Ok 空响应。

因为资源可用但是路径变量的记录不存在所以这里return404不是一个好的选择