preface
Eager to solve the problem now, immediately, immediately, please pull directly to the bottom [solution]!! I hope I can help you.
Xiaobian recently did a job, is to transplant the company a set of old projects to the new framework, the so-called “new three years, the old three years, sewing three years”, re-decoration, convenient company price to sell again. Then you run into this “dispatcherServlet” exception problem.
At first I thought there was a problem with SpringBoot’s underlying framework, after all it was DispatcherServlet involved, but I was in tears when I learned the truth.
Problem description
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
Abnormal screenshot:
Cause analysis,
There was a servlet load/call failure at JVM compile time.
I drew a diagram to review the DispatcherServlet with you:
The whole process can be roughly described as follows:
- An HTTP request arrives at the server and is received by a DispatcherServlet.
- The DispatcherServlet delegates requests to the appropriate processor Controller, at which point processing control reaches the Controller object.
- The Controller completes the creation of the data model of the request and the processing of the business logic internally, and then returns the model filled with the data, namely the model and the control to the DispatcherServlet, and assigns the DispatcherServlet to render the response.
- The DispatcherServlet then combines this data with the appropriate data template view to output a Response to the Response.
The solution
After solving the problem, I studied it carefully and made the following summary based on the experience and lessons of many predecessors, which is very comprehensive:
1. Compilation error occurs
Cause analysis: For unknown reasons, the JVM did not compile the newly created classes, forcing the project to be refactored.
Solution :(for example, IDEA) navigation bar > Build > ReBuild Project
2. Forget to annotate @resource or @autowired on the Controllor interface
Cause analysis: The reason is the same as [1], but the difference is that it is artificially caused that a certain class cannot be found during project compilation
Solution: Go through the program and add @AutoWired or @Resource annotations to the introduced Service layer implementation class
3. The Controllor layer calls methods that are private, causing distribution failures
Reason analysis: I have not studied the old framework, so I dare not talk about it, but MVC, SpringBoot, SpringBootCloud framework are required to be public
Solution: Find the error method and change the method of the interface class to public, as shown below ↓↓↓
4. Others, such as: SpringBoot annotations are not used correctly
Specific problems to specific analysis, the description of the problem is written clearly in the exception, for example, my exception is “… Java. Lang. NullPointerException, null, “this is obviously a little something, so we have the possibility of” 1 ~ 3 “above.
DispatcherServlet exception “… Java. Lang. IllegalArgumentException: Unknown return value type: Java. Lang. Integer “.
A similar problem is with the annotation we used, such as forgetting to annotate @responseBody on an interface of the Controllor layer
@responseBody What annotations do: After converting the object returned by the Controller method to the specified format through the appropriate converter, it is written to the body of the Response object, which is usually used to return JSON data or XML data. Note that after using this annotation, no further attempts are made to the processor. Instead, it writes the data directly to the input stream, which has the same effect as writing the data in the specified format through the Response object.
In conclusion, it is the incorrect use of annotations that causes the inconsistency of the data format between the front and back ends. This still needs to be paid attention to.
conclusion
- Specific problems should be analyzed, the description of the problem is clearly written in the exception, do not only focus on the DispatcherServlet, the following description is the key to locate the problem.
- Xiaobian encountered is the first [3] kind of problem, because it is a transplant of an old project, clumsy, did not see, also did not think, looking for a long time to find!! ε=(´ο ‘*)))