我无法运行我的 Spring Boot 应用程序,因为 JPA 存在问题。但我无法找到问题并解决它。我在几个堆栈溢出主题上搜索了解决方案,但没有一个解决方案对我有帮助。我定义了以下模型:
我的实体类的代码-
package com.ncu.legalVise.entities;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Entity
@Table(name ="users")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Users {
// public Users(String username, String password, int enabled) {
// super();
// this.username = username;
// this.password = password;
// this.enabled = enabled;
// }
//
// public Users() {
// super();
// }
@Id
private String username;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private int enabled;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getEnabled() {
return enabled;
}
public void setEnabled(int enabled) {
this.enabled = enabled;
}
}
我的 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ncu</groupId>
<artifactId>LegalVise</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>LegalVise</name>
<description>Legal Vise</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
我的控制器-
package com.ncu.legalVise.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ncu.legalVise.entities.Authorities;
import com.ncu.legalVise.entities.Users;
import com.ncu.legalVise.repository.AuthoritiesRepository;
import com.ncu.legalVise.repository.UsersRepository;
@RestController
@RequestMapping("/api/public/security")
public class SecurityController {
Users user;
Authorities authority;
UsersRepository usersRepository;
AuthoritiesRepository authoritiesRepository;
@Autowired
SecurityController(Users user, Authorities authority,UsersRepository usersRepository,AuthoritiesRepository authoritiesRepository)
{
user = this.user;
authority= this.authority;
usersRepository=this.usersRepository;
authoritiesRepository=this.authoritiesRepository;
}
@PostMapping("/register")
public ResponseEntity<Object> register(@RequestBody String userName, @RequestBody String password, @RequestBody String authorities)
{
user.setUsername(userName);
user.setPassword(password);
user.setEnabled(1);
authority.setUsername(userName);
authority.setAuthority(authorities);
if(!usersRepository.existsById(userName))
{
usersRepository.save(user);
authoritiesRepository.save(authority);
return ResponseEntity.ok(usersRepository.findById(userName));
}
return ResponseEntity.notFound().build();
}
}
错误信息-
应用程序无法启动
描述:
com.ncu.legalVise.controller.SecurityController 中构造函数的参数 0 需要类型为“com.ncu.legalVise.entities.Users”的 bean,但无法找到。
行动:
考虑在您的配置中定义“com.ncu.legalVise.entities.Users”类型的 bean。
请帮我解决这个问题
我尝试更改依赖项,但没有任何效果
问题出在控制器构造函数上。应该是这样的:
SecurityController(Users user, Authorities authority,UsersRepository usersRepository,AuthoritiesRepository authoritiesRepository)
{
this.user = user;
this.authority = authority;
this.usersRepository = usersRepository;
this.authoritiesRepository = authoritiesRepository;
}
由于您的目标是 SpringBoot 3,因此您应该在
javax.persistence
类中将 jakarta.persistence
更改为 Users
导入。
SpringBoot 3 的主要变化是采用 JakartaEE 9(放弃了 jdk8 / jdk11 支持以拥有 jdk17 基线),这导致许多 JavaEE 功能(包括 JPA)必须从
javax
迁移到 jakarta
。
自从您的代码编译后,我还建议清理您的
pom.xml
以删除旧的JPA依赖项,以便删除javax.persistence
识别的包(以及旧的JavaEE8 jpa支持)。