【问题标题】:Spring boot web app gives 500 internal server error instead 404 not foundSpring Boot Web 应用程序给出 500 内部服务器错误,而不是 404 未找到
【发布时间】:2021-06-18 14:07:45
【问题描述】:

如果没有给定 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;
    } 

我的异常类:

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

【问题讨论】:

标签: java spring spring-boot exception http-status-code-404


【解决方案1】:

发生这种情况是因为 Optional 通过以下方式返回: repository.findById(id) 可以返回一个empty,所以如果你在它上面调用get(),它会抛出一个java.util.NoSuchElementException

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

4XX 错误代码与客户端错误有关,您不应将此作为最佳实践返回。您可以返回 200 Ok 并返回空响应。

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

【讨论】:

    猜你喜欢
    • 2012-04-06
    • 2019-07-10
    • 1970-01-01
    • 2020-05-14
    • 2018-03-08
    • 2014-10-16
    • 2020-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多