我一直在试图解决这个问题,在过去的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
这似乎是我的地址文件中有什么问题,但我不能看到为什么,我将粘贴代码在这里:
地址控制器:
package com.factory.ordersystem.controller;
import com.factory.ordersystem.entities.Address;
import com.factory.ordersystem.service.AddressService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/addresses")
public class AddressController {
private final AddressService addressService;
public AddressController(AddressService addressService) {
this.addressService = addressService;
}
@GetMapping("/{id}")
public ResponseEntity<Address> getAddressById(@PathVariable Long id) {
Optional<Address> address = addressService.getAddressById(id);
return address != null ?
(ResponseEntity<Address>) ResponseEntity.ok() :
ResponseEntity.notFound().build();
}
@GetMapping
public ResponseEntity<List<Address>> getAllAddresses() {
List<Address> addresses = addressService.getAllAddresses();
return ResponseEntity.ok(addresses);
}
@PostMapping
public ResponseEntity<Address> createAddress(@RequestBody Address address) {
Address createdAddress = addressService.createAddress(address);
return ResponseEntity.status(HttpStatus.CREATED).body(createdAddress);
}
@PutMapping("/{id}")
public ResponseEntity<Address> updateAddress(@PathVariable Long id, @RequestBody Address updatedAddress) {
Optional<Address> updated = Optional.ofNullable(addressService.updateAddress(id, updatedAddress));
return updated.map(ResponseEntity::ok)
.orElse(ResponseEntity.notFound().build());
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteAddress(@PathVariable Long id) {
boolean deleted = addressService.deleteAddress(id);
return deleted ?
ResponseEntity.noContent().build() :
ResponseEntity.notFound().build();
}
}
字符串
地址实体文件
package com.factory.ordersystem.entities;
import javax.persistence.*;
import javax.validation.constraints.NotBlank;
@Entity
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
private String street;
@NotBlank
private String city;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id")
private Customer customer;
public Address() {
}
public Address(String street, String city) {
this.street = street;
this.city = city;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Customer getCustomer() {
return customer;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
@Override
public String toString() {
return "Address{" +
"id=" + id +
", street='" + street + '\'' +
", city='" + city + '\'' +
'}';
}
}
型
地址储存库
package com.factory.ordersystem.repository;
import com.factory.ordersystem.entities.Address;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface AddressRepository extends JpaRepository<Address, Long> {
}
型
地址服务
package com.factory.ordersystem.service;
import com.factory.ordersystem.entities.Address;
import com.factory.ordersystem.exception.ResourceNotFoundException;
import com.factory.ordersystem.repository.AddressRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class AddressService {
private final AddressRepository addressRepository;
@Autowired
public AddressService(AddressRepository addressRepository) {
this.addressRepository = addressRepository;
}
public List<Address> getAllAddresses() {
return addressRepository.findAll();
}
public Optional<Address> getAddressById(Long id) {
return addressRepository.findById(id);
}
public Address createAddress(Address address) {
return addressRepository.save(address);
}
public Address updateAddress(Long id, Address updatedAddress) {
Optional<Address> optionalAddress = addressRepository.findById(id);
if (optionalAddress.isPresent()) {
Address existingAddress = optionalAddress.get();
existingAddress.setStreet(updatedAddress.getStreet());
existingAddress.setCity(updatedAddress.getCity());
return addressRepository.save(existingAddress);
} else {
throw new ResourceNotFoundException("Address with id " + id + " not found");
}
}
public boolean deleteAddress(Long id) {
if (addressRepository.existsById(id)) {
addressRepository.deleteById(id);
return true;
} else {
return false;
}
}
}
型
application.properties
# DataSource Configuration
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.driverClassName=org.h2.Driver
# Hibernate Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.open-in-view=false
# H2 Console Configuration
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
型
最后是POM.xml
<?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.factory.ordersytem</groupId>
<artifactId>com.exam.pgr209</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>com.exam.pgr209</name>
<description>order machine backend exam</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version> <!-- or the latest version -->
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
型
file structure
任何帮助都将是疯狂的赞赏,因为它将减轻我最头痛的过去2周
我尝试了各种故障排除步骤:
使用虚拟机管理器。重新构造文件组织。从头开始创建新文件。观看关于“非托管类型”错误的视频教程。尝试集成Lombok。将我们的考试项目结构与已知的工作项目(如“vet-clinic”)进行比较。进行广泛的在线研究。仔细检查潜在的印刷错误。使用安全扫描配置。尽管进行了这些尝试,但问题仍然存在。
然而,可能是我缺乏技能,使我做错了这一点,
1条答案
按热度按时间5ssjco0h1#
这个问题是由于库javax-persistence(旧库)和jakarta-persistence(当前库)之间的版本冲突造成的。要解决这个问题,您应该从您的
pom.xml
中删除此依赖项:字符串
完成此操作并更新Maven项目后,您将在使用javax. persistence的导入语句中获得编译器错误。只需将其重命名为jakarta. persistence。
验证库也存在类似问题。请删除此依赖项:
型
用这个代替:
型
一般建议:获取正确且最新的
pom.xml
的最佳方法是使用https://start.spring.io/生成它