[英]Spring Boot - javax Import statements not working correctly
I just started working with Spring Boot , and am currently following this tutorial with some small modifications to naming and using their own version of Eclipse
, which has this generator built in.我刚开始使用Spring Boot ,目前正在关注本教程,对命名和使用他们自己的
Eclipse
版本进行了一些小的修改,该版本内置了这个生成器。
When I get to the first snippet of code, I try just copying over the import statements to start with,当我到达第一段代码时,我尝试复制导入语句以开始,
import com.fasterxml.jackson.annotation.JsonIgnore;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.HashSet;
import java.util.Set;
which gets me the following error with the javax imports:这让我在 javax 导入时出现以下错误:
The import javax.persistence.[insert name here] cannot be resolved
When I find the javax.persistence
package, I find that, sure enough, the starter code provided from their own service does not contain the listed packages.当我找到
javax.persistence
包时,我发现,果然,他们自己的服务提供的启动代码不包含列出的包。 I'm left confused and wondering if I did something wrong in the initial steps.我很困惑,想知道我在最初的步骤中是否做错了什么。 Anyone have any ideas?
有人有想法么?
Edit 1: pom content provided编辑 1:提供的 pom 内容
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.Me</groupId>
<artifactId>petstore</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>petstore</name>
<description>Petstore Project</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Add spring-boot-starter-data-jpa
dependency添加
spring-boot-starter-data-jpa
依赖
If you are using Maven
add to pom.xml如果你使用
Maven
添加到pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
If you are using Gradle
add in build.gradle如果您使用
Gradle
,请添加build.gradle
compile "org.springframework.boot:spring-boot-starter-data-jpa"
Please refer the docs.请参考文档。 https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes
As of #19550, Web and WebFlux starters do not depend on the validation starter by default anymore.
从 #19550 开始,Web 和 WebFlux 启动器默认不再依赖验证启动器。 If your application is using validation features, for example you find that javax.validation.* imports are not being resolved, you'll need to add the starter yourself.
如果您的应用程序正在使用验证功能,例如您发现 javax.validation.* 导入没有被解析,您需要自己添加启动器。
For Maven builds, you can do that with the following:
对于 Maven 构建,您可以执行以下操作:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>
For Gradle, you will need to add something like this:
对于 Gradle,您需要添加如下内容:
dependencies { ... implementation 'org.springframework.boot:spring-boot-starter-validation' }
In my case i have added below spring boot data dependency but still not be able to import javax.persistence.*
在我的情况下,我在 spring boot 数据依赖项下添加了但仍然无法导入
javax.persistence.*
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
I realized that eclipse somehow not be able to adding this dependencies to my classpath.我意识到 Eclipse 不知何故无法将此依赖项添加到我的类路径中。 Firstly i tried right click on the project
Maven>Update Project
but still not worked.首先,我尝试右键单击项目
Maven>Update Project
,但仍然没有工作。 Then I execute mvn eclipse:eclipse
inside the root directory and then after successful build i refreshed project in the eclipse.然后我在根目录中执行
mvn eclipse:eclipse
,然后在成功构建后我在 eclipse 中刷新了项目。 This time it worked.这次奏效了。
Add this dependency in you pom.xml在 pom.xml 中添加此依赖项
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
After that Right click on your project and in maven option update your project.之后右键单击您的项目并在 Maven 选项中更新您的项目。 That should fix it.
那应该解决它。
I had the same issue, but previous solutions didn't work for me.我有同样的问题,但以前的解决方案对我不起作用。 I do solve the issue by adding following dependency to POM file.
我确实通过向 POM 文件添加以下依赖项来解决这个问题。
<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
Perhaps this will help someone: apparently Java persistence api is renamed to Jakarta persistence.也许这会对某人有所帮助:显然 Java 持久性 API 已重命名为 Jakarta 持久性。 So you will have to import Jakarta persistence instead of javax.persistence.
所以你必须导入 Jakarta persistence 而不是 javax.persistence。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.