IntelliJ无法识别某些Spring包。

huangapple go评论94阅读模式
英文:

IntelliJ does not recognize certain Spring packages

问题

在我的REST API项目中,IntelliJ突然停止识别来自org.springframework.http和org.springframework.web包的导入。例如,当我尝试创建ResponseEntity的实例或使用@RestController注解时,它会给我显示红色波浪线。当我将鼠标悬停在相应的导入语句上时,会显示“无法解析符号”的消息。

我点击灯泡图标并选择“添加Maven依赖项”,但IntelliJ找不到要添加的依赖项。即使它可以找到,正确的Maven依赖项已经存在于pom.xml中,但它们被灰掉,好像它们没有被引用到任何地方。手动指定不同版本的依赖项也没有效果。重新导入项目也没有效果。再次克隆存储库并启动新项目也没有帮助。删除缓存、删除整个.m2目录、更新IntelliJ、重新启动计算机或更改解释器也没有帮助。

代码在终端和IDE中都按预期构建和运行,没有任何问题。我不知道为什么IntelliJ看起来如此困惑。这只发生在我的计算机上,我想不出任何可能导致这种情况的原因。团队中的其他人没有遇到这个问题。它不是局限于特定的存储库。每次我尝试从这些特定的包中导入时都会发生。这个问题已经困扰了我一整周,我的队友和我一样感到困惑。有什么建议吗?

附加信息:

  • Java 11.0.6
  • 最新版本的Maven和IntelliJ
  • macOS Mojave

更新

我决定再次删除我的整个.m2目录和项目的.idea目录。我在过去的一周多次这样做,但没有结果。这一次,当我重新导入项目时,我发现前面提到的org.springframework.web和org.springframework.http导入现在被正确识别。然而,IntelliJ现在无法识别一些其他的导入,比如lombok。

上周,我遇到了这个特定版本的问题,持续了好几个小时。然后我休息了一会,打开了我的笔记本电脑,发现一切都神奇地恢复正常了。几天后,我开始遇到了我上面描述的错误。这些未识别的导入似乎会随机出现和消失,而不管我正在工作的项目或代码的任何更改。

让我再次强调,代码没有任何问题。IntelliJ可以完美地构建和运行它,即使有所有这些红色波浪线。编辑器似乎有些问题。我在下面包含了当前状态的截图。

IntelliJ无法识别某些Spring包。

已解决

我决定卸载并重新安装IntelliJ,问题得到了解决。

英文:

<p>
In my rest API projects, IntelliJ suddenly stopped recognizing imports from the packages org.springframework.http and org.springframework.web. For example, it gives me the red squiggleys when I try to create an instance of ResponseEntity or use the @RestController annotation. I also get the red squiggleys under the corresponding import statements with the message “Cannot resolve symbol” when I hover my cursor over them.

I click the light bulb and select “add Maven dependency”, but IntelliJ cannot find any to add. Even if it could, the proper Maven dependencies are already present in the pom.xml, but they are grayed-out as if they aren’t referenced anywhere. Manually specifying different versions of the dependencies does nothing. Reimporting the project does nothing. Cloning the repo again and starting a new project didn’t help. Neither did invalidating the caches, deleting the entire .m2 directory, updating IntelliJ, restarting my machine, or fiddling with the interpreter.

The code builds and runs as expected without any problems both in terminal and in the IDE. I have no idea why the IntelliJ seems so confused. This is only happening on my machine, and I can’t think of anything I could have done to cause it. Nobody else on the team has seen this issue. It’s not isolated to any particular repo. It happens any time I try to import from these specific packages. It’s been bugging me all week, and my teammates are just as bewildered as I am. Any suggestions?

Additional Info:

  • Java 11.0.6
  • Latest versions of Maven and IntelliJ
  • macOS Mojave
    <p/>

<br>

<p>

Update

I decided to once again delete my entire .m2 directory and the .idea directory for my project. I have done this multiple times over the past week with no results. This time, when I reimported the project I found the aforementioned org.springframework.web and org.springframework.http imports are now being recognized correctly. However, IntelliJ is now failing to recognize some of the other imports, such as lombok.

I actually had this particular version of the problem last week for several hours. I then took a short break and opened my laptop to find that everything was magically back to normal. A few days later, I started having the error that I described above. These unrecognized imports seem to appear and disappear at random, regardless of the project I am working in or any changes to the code.

Let me reiterate that there is nothing wrong with the code. IntelliJ builds and runs it flawlessly, even with all the red squiggleys. There is something wonky going on with the editor. I have included a screenshot below of the current state.
<p/>

IntelliJ无法识别某些Spring包。

Solved

I decided to uninstall and reinstall IntelliJ. which resolved the issue.

答案1

得分: 1

这可能有很多原因:

尝试以下操作:

(Mac用户)右键单击项目-> Maven->重新导入

如果这不起作用:

删除.idea文件夹,然后重新导入项目。
它位于项目的根目录,就像这样

za$ ls .idea/
.name compiler.xml encodings.xml httpRequests/ misc.xml vcs.xml workspace.xml

如果这不起作用,请确保jar已加载并作为依赖项列出,或者手动导入它。请查看此问题:

https://stackoverflow.com/questions/1051640/correct-way-to-add-external-jars-lib-jar-to-an-intellij-idea-project

如果这不起作用:

./m2文件夹中删除所有jar的所有版本(存在问题的jar)。或者确保在pom.xml中指定特定版本。

英文:

This can happen for lots of reasons:

Try the following:

(Mac users) Right click on the project -&gt; Maven -&gt; reimport

If that doesn't work:

Delete `.idea` folder and re import the project.
Its located in the root of the project. like this one

za$ ls .idea/
.name          compiler.xml   encodings.xml  httpRequests/  misc.xml       vcs.xml        workspace.xml  

If that doesn't work, make sure the jar is loaded and you can see it listed as a dependency, or import it manually. see this questions:

https://stackoverflow.com/questions/1051640/correct-way-to-add-external-jars-lib-jar-to-an-intellij-idea-project

If that doesn't work:

> Delete all versions of the jar(that having the issue) from ./m2
> folder. Or make sure to specify a specific version in pom.xml

答案2

得分: 0

请参考此处的图片链接从导入和完成中排除

在IntelliJ设置中,依次点击"File -> Settings",然后进入"Editor -> General -> Auto Import -> Java section -> Exclude from import and completion"。移除您不小心添加进去的那些类。这可能是因为在IntelliJ提供自动建议时,您曾经误按了错误的按钮。

实际上,我几天前也遇到了同样的问题,这是我是如何解决的。

英文:

Refer to the image link here Exclude from import and Completion

In Intellij settings File-> Settings, go to Editor -> General -> Auto Import -> Java section -> Exclude from import and completion. Remove those classes you have accidentally put there. It might have ever happened coz you have ever pressed the wrong button when there is an auto-suggest from Intellij.

Actually, I have the same problem a few days ago. And that is how I resolved it.

huangapple
  • 本文由 发表于 2020年8月1日 08:18:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63200569.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定