Spring Boot Sping Boot (使用JPA的maven和内存中的H2数据库)不断获取非托管类型””,并在创建名为””的Bean时出错

lzfw57am  于 2024-01-06  发布在  Spring
关注(0)|答案(1)|浏览(350)

我一直在试图解决这个问题,在过去的2天,似乎无法解决他们
下面是Spring给我的错误代码:
2023-12- 16 T23:43:15.665+01:00 WARN 22472 - [ restartedMain] ConfigServletWebServerApplicationContext:在上下文初始化过程中遇到异常-取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:在文件[C:\target\classes\com\factory\ordersystem\Weber\AddressController.class]中定义名为“addressController”的bean时出错:通过构造函数参数0表示的依赖不满足:创建文件[C:\target\classes\com\factory\ordersystem\service\AddressService.class]中定义的名为“addressService”的Bean时出错:通过构造函数参数0表示的依赖项不满足:创建在com.factory.ordersystem.repository中定义的名为“addressRepository”的Bean时出错。AddressRepository定义在@ EnableJpaRepositories在JpaRepositoriesRegistrar上声明。EnableJpaRepositoriesConfiguration:不是托管类型:class com.factory.ordersystem.entities.Address
这似乎是我的地址文件中有什么问题,但我不能看到为什么,我将粘贴代码在这里:
地址控制器:

  1. package com.factory.ordersystem.controller;
  2. import com.factory.ordersystem.entities.Address;
  3. import com.factory.ordersystem.service.AddressService;
  4. import org.springframework.http.HttpStatus;
  5. import org.springframework.http.ResponseEntity;
  6. import org.springframework.web.bind.annotation.*;
  7. import java.util.List;
  8. import java.util.Optional;
  9. @RestController
  10. @RequestMapping("/addresses")
  11. public class AddressController {
  12. private final AddressService addressService;
  13. public AddressController(AddressService addressService) {
  14. this.addressService = addressService;
  15. }
  16. @GetMapping("/{id}")
  17. public ResponseEntity<Address> getAddressById(@PathVariable Long id) {
  18. Optional<Address> address = addressService.getAddressById(id);
  19. return address != null ?
  20. (ResponseEntity<Address>) ResponseEntity.ok() :
  21. ResponseEntity.notFound().build();
  22. }
  23. @GetMapping
  24. public ResponseEntity<List<Address>> getAllAddresses() {
  25. List<Address> addresses = addressService.getAllAddresses();
  26. return ResponseEntity.ok(addresses);
  27. }
  28. @PostMapping
  29. public ResponseEntity<Address> createAddress(@RequestBody Address address) {
  30. Address createdAddress = addressService.createAddress(address);
  31. return ResponseEntity.status(HttpStatus.CREATED).body(createdAddress);
  32. }
  33. @PutMapping("/{id}")
  34. public ResponseEntity<Address> updateAddress(@PathVariable Long id, @RequestBody Address updatedAddress) {
  35. Optional<Address> updated = Optional.ofNullable(addressService.updateAddress(id, updatedAddress));
  36. return updated.map(ResponseEntity::ok)
  37. .orElse(ResponseEntity.notFound().build());
  38. }
  39. @DeleteMapping("/{id}")
  40. public ResponseEntity<Void> deleteAddress(@PathVariable Long id) {
  41. boolean deleted = addressService.deleteAddress(id);
  42. return deleted ?
  43. ResponseEntity.noContent().build() :
  44. ResponseEntity.notFound().build();
  45. }
  46. }

字符串
地址实体文件

  1. package com.factory.ordersystem.entities;
  2. import javax.persistence.*;
  3. import javax.validation.constraints.NotBlank;
  4. @Entity
  5. public class Address {
  6. @Id
  7. @GeneratedValue(strategy = GenerationType.IDENTITY)
  8. private Long id;
  9. @NotBlank
  10. private String street;
  11. @NotBlank
  12. private String city;
  13. @ManyToOne(fetch = FetchType.LAZY)
  14. @JoinColumn(name = "customer_id")
  15. private Customer customer;
  16. public Address() {
  17. }
  18. public Address(String street, String city) {
  19. this.street = street;
  20. this.city = city;
  21. }
  22. public Long getId() {
  23. return id;
  24. }
  25. public void setId(Long id) {
  26. this.id = id;
  27. }
  28. public String getStreet() {
  29. return street;
  30. }
  31. public void setStreet(String street) {
  32. this.street = street;
  33. }
  34. public String getCity() {
  35. return city;
  36. }
  37. public void setCity(String city) {
  38. this.city = city;
  39. }
  40. public Customer getCustomer() {
  41. return customer;
  42. }
  43. public void setCustomer(Customer customer) {
  44. this.customer = customer;
  45. }
  46. @Override
  47. public String toString() {
  48. return "Address{" +
  49. "id=" + id +
  50. ", street='" + street + '\'' +
  51. ", city='" + city + '\'' +
  52. '}';
  53. }
  54. }


地址储存库

  1. package com.factory.ordersystem.repository;
  2. import com.factory.ordersystem.entities.Address;
  3. import org.springframework.data.jpa.repository.JpaRepository;
  4. import org.springframework.stereotype.Repository;
  5. @Repository
  6. public interface AddressRepository extends JpaRepository<Address, Long> {
  7. }


地址服务

  1. package com.factory.ordersystem.service;
  2. import com.factory.ordersystem.entities.Address;
  3. import com.factory.ordersystem.exception.ResourceNotFoundException;
  4. import com.factory.ordersystem.repository.AddressRepository;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. import java.util.List;
  8. import java.util.Optional;
  9. @Service
  10. public class AddressService {
  11. private final AddressRepository addressRepository;
  12. @Autowired
  13. public AddressService(AddressRepository addressRepository) {
  14. this.addressRepository = addressRepository;
  15. }
  16. public List<Address> getAllAddresses() {
  17. return addressRepository.findAll();
  18. }
  19. public Optional<Address> getAddressById(Long id) {
  20. return addressRepository.findById(id);
  21. }
  22. public Address createAddress(Address address) {
  23. return addressRepository.save(address);
  24. }
  25. public Address updateAddress(Long id, Address updatedAddress) {
  26. Optional<Address> optionalAddress = addressRepository.findById(id);
  27. if (optionalAddress.isPresent()) {
  28. Address existingAddress = optionalAddress.get();
  29. existingAddress.setStreet(updatedAddress.getStreet());
  30. existingAddress.setCity(updatedAddress.getCity());
  31. return addressRepository.save(existingAddress);
  32. } else {
  33. throw new ResourceNotFoundException("Address with id " + id + " not found");
  34. }
  35. }
  36. public boolean deleteAddress(Long id) {
  37. if (addressRepository.existsById(id)) {
  38. addressRepository.deleteById(id);
  39. return true;
  40. } else {
  41. return false;
  42. }
  43. }
  44. }


application.properties

  1. # DataSource Configuration
  2. spring.datasource.url=jdbc:h2:mem:testdb
  3. spring.datasource.username=sa
  4. spring.datasource.password=password
  5. spring.datasource.driverClassName=org.h2.Driver
  6. # Hibernate Configuration
  7. spring.jpa.hibernate.ddl-auto=update
  8. spring.jpa.open-in-view=false
  9. # H2 Console Configuration
  10. spring.h2.console.enabled=true
  11. spring.h2.console.path=/h2-console


最后是POM.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>3.2.0</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.factory.ordersytem</groupId>
  12. <artifactId>com.exam.pgr209</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>com.exam.pgr209</name>
  15. <description>order machine backend exam</description>
  16. <properties>
  17. <java.version>21</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-jpa</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-devtools</artifactId>
  31. <scope>runtime</scope>
  32. <optional>true</optional>
  33. </dependency>
  34. <dependency>
  35. <groupId>com.h2database</groupId>
  36. <artifactId>h2</artifactId>
  37. <scope>runtime</scope>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.springframework.boot</groupId>
  41. <artifactId>spring-boot-starter-test</artifactId>
  42. <scope>test</scope>
  43. </dependency>
  44. <dependency>
  45. <groupId>javax.persistence</groupId>
  46. <artifactId>javax.persistence-api</artifactId>
  47. <version>2.2</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>javax.validation</groupId>
  51. <artifactId>validation-api</artifactId>
  52. <version>2.0.1.Final</version> <!-- or the latest version -->
  53. </dependency>
  54. </dependencies>
  55. <build>
  56. <plugins>
  57. <plugin>
  58. <groupId>org.springframework.boot</groupId>
  59. <artifactId>spring-boot-maven-plugin</artifactId>
  60. </plugin>
  61. </plugins>
  62. </build>
  63. </project>



file structure

任何帮助都将是疯狂的赞赏,因为它将减轻我最头痛的过去2周
我尝试了各种故障排除步骤:
使用虚拟机管理器。重新构造文件组织。从头开始创建新文件。观看关于“非托管类型”错误的视频教程。尝试集成Lombok。将我们的考试项目结构与已知的工作项目(如“vet-clinic”)进行比较。进行广泛的在线研究。仔细检查潜在的印刷错误。使用安全扫描配置。尽管进行了这些尝试,但问题仍然存在。
然而,可能是我缺乏技能,使我做错了这一点,

5ssjco0h

5ssjco0h1#

这个问题是由于库javax-persistence(旧库)和jakarta-persistence(当前库)之间的版本冲突造成的。要解决这个问题,您应该从您的pom.xml中删除此依赖项:

  1. <dependency>
  2. <groupId>javax.persistence</groupId>
  3. <artifactId>javax.persistence-api</artifactId>
  4. <version>2.2</version>
  5. </dependency>

字符串
完成此操作并更新Maven项目后,您将在使用javax. persistence的导入语句中获得编译器错误。只需将其重命名为jakarta. persistence。
验证库也存在类似问题。请删除此依赖项:

  1. <dependency>
  2. <groupId>javax.validation</groupId>
  3. <artifactId>validation-api</artifactId>
  4. <version>2.0.1.Final</version> <!-- or the latest version -->
  5. </dependency>


用这个代替:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-validation</artifactId>
  4. </dependency>


一般建议:获取正确且最新的pom.xml的最佳方法是使用https://start.spring.io/生成它

展开查看全部

相关问题