当前位置:网站首页>Share an experience of self positioning + problem solving

Share an experience of self positioning + problem solving

2022-06-27 20:56:00 I am fat tiger

The problem background

Recently in use Java Of Springboot Framework development a project , encounter 2 individual An unsolved problem . At that time, I was confused by this problem , To solve this 2 individual bug Explored for hours , Finally, I fell down and groped by myself , Then locate the problem and solve it ! Although it turns out that the solution to the problem is simple , But position yourself , The process of analyzing problems , And then solve the problem , Full of sense of achievement , ha-ha , So share the whole process ~

Problem summary

All in all 2 A question :

first : Wrote a very basic Controller Class and then access , The result is always 404

the second : First, in the Maven It is written in the parent project Swagger Configuration of , Created 2 individual Maven Sub project .

  • In the sub project A This parent class is introduced in , It can be accessed normally swagger Of web Interface .
  • Sub project B This parent class is also introduced , Sub project B Medium Pom.xml Configuration and sub projects A It's almost the same , But it can not be accessed normally swagger Of web Interface ...

Actual problem description

This is the description of the first problem

  1. It was written at that time Controller
  1. According to common sense , I just need external access http://127.0.0.1:8002/oss/file/w You can get the expected return value , However, the actual results are as follows , return 404 error

3. Start troubleshooting

  • Use postman Debug interface , postman The interface is as follows
  • Because multiple services are started at the same time , So I first check whether the service visited is correct . Confirmed , The startup port of the target service is 8002 server.port=8002, then postman Also visited 8002 port
  • Confirm whether the access route is correct First, it looks like postman The requested route seems to be ok , The route exactly matches . But after several actual requests , It turns out that 404 after , Manually copy the route in the code to postman On , Prevent problems caused by dazzle or Chinese characters . The results have not changed , still 404 ~
  • The suspicion is idea The cache problem of ? With this skepticism , Try to restart idea Several times , The result remains the same , Ah , At this point, the mentality is a little impatient
  • I have no idea , Try Baidu

After reading this post written by the great God , Suddenly, I have some ideas , So check the application @ComponentScan annotation . It is no problem to simply read the notes , Then try to look at the package where the file is located , Find out ...

  • Problem analysis

After investigation and positioning , The problem is obvious , The reason is that the @ComponentScan The internal configuration is incorrect !( When writing the package name, it should be com.atguuigu.oss, But an extra one u Turned into com.atguuigu.oss)

because @ComponentScan The configuration inside does not correspond to the actual package name , So when the application starts , Spring The... Under the correct package name directory is not automatically loaded Controller file , So external access will occur 404, Logical ~

  • Try to solve the problem

Because the post just said : If not configured @ComponentScan, Springboot It will automatically load all components under the package where the startup class is located and its sub packages

  1. So first we will start the above class @ComponentScan(basePackages = "com.atguigu") This sentence has been deleted , Verify if it is normal . After restart verification , Interface access is normal , hahaha~
  1. The operation just now , Initial results have been achieved . But another problem is swagger-ui Of web Interview questions . Hold the idea of following the vine and feeling the melon , Guess access swagger This is probably the same reason !

Just now it was directly removed @ComponentScan The way to solve the problem , Another way is to @ComponentScan(basePackages = "com.atguigu") The configuration inside should be consistent with the package name .

Operating results : After the change , New services swagger Successful visit !!!

Today's summary

  1. Although it looks like a long and minute statement Problems encountered in the troubleshooting of , then If it is actually solved, it will be changed to a letter ( It is estimated that someone should spray such vegetables , Just because of a letter , It will take hours to troubleshoot ?)
  2. Want to pass this article , Share your solutions to the problems you encounter : Check step by step + solve the problem ~
  3. Share a few famous words :
  • I don't know the truth , Only the body in this mountain !
  • Mountains and rivers lead nowhere , Another village !

Because when writing this article , I realized the connotation of these two poems , haha~

  1. Share today's insights
  • If you're in a bad mood , You need to calm down first , And then do something else , Otherwise, you will get half the result !
  • Don't worry when things happen , Try to be calm , This will keep the brain calm , See things rationally . There needs to be a kind of “ Mount Tai collapses in front of us, but its color remains the same , The elk is fond of the left but not the eye ” State of mind !
原网站

版权声明
本文为[I am fat tiger]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/178/202206271840140431.html