异常:Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception

Error report :

ERROR http-nio-8080-exec-1 (DirectJDKLog.java:175) - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed;

nested exception is java.lang.NullPointerException] with root cause java.lang.NullPointerException: null

 Problem solved :

1. Analysis of the cause of the compilation error
        : the reason is unknown, the JVM did not compile the newly created class, and the project needs to be refactored forcibly (it can be understood as shutting down and restarting the computer)

        Solution: (IDEA as an example) navigation bar --> Build --> ReBuild Project

2. I forgot to write @Resource or @Autowired annotations on the Service interface introduced by the Controllor layer
        Reason analysis: The reason is the same as [1], the difference is that this is caused by human beings, and a certain class cannot be found during project compilation

        Solution: Check the program carefully and add annotation @Autowired or @Resource annotation on the introduced Service layer implementation class

3. The method called by the Controllor layer is private, which leads to
        the analysis of the reasons for the failure of distribution: I have not researched why the old framework can be used, so I dare not talk nonsense, but the MVC, SpringBoot, and SpringBootCloud frameworks are all required to be public

        Solution: Find the wrong method and change the method of the interface class to public, as shown in the figure below↓↓↓

 

4. The Mock test does not pass parameters or the parameters are wrongly written.
        The experience of the big guys in the comment area is included. Please carefully check whether the parameter names are mapped correctly.

        I have not reproduced this problem locally, so I write it here, hoping that everyone can provide a solution.

5. SpringBoot annotations are not used correctly
        . Specific problems need to be analyzed in detail. The description of the problem is clearly written in the exception. For example, my exception is "... java.

        The editor also saw that someone had a DispatcherServlet exception described as "...java.lang.IllegalArgumentException: Unknown return value type: java.lang.Integer".

        Similar to this kind of problem, there is a problem with the annotation we use, such as: forgetting to add the @ResponseBody annotation on an interface of the Controller layer

The role of the @responseBody annotation:

        After the object returned by the controller method is converted into the specified format through an appropriate converter, it is written into the body area of ​​the response object, which is usually used to return JSON data or XML data;

        It should be noted that after using this annotation, it will not go to the processor, but will directly write the data to the input stream. Its effect is equivalent to outputting the data in the specified format through the response object.

Summarize the cause of the error: the incorrect use of annotations caused the inconsistency of the front-end and back-end data formats

Guess you like

Origin blog.csdn.net/weixin_64854388/article/details/131738417