目前我正在尝试运行我的 Spring Boot 应用程序。我遵循了教程:https://www.youtube.com/watch?v=BtUdl9pZwR0
不幸的是我收到错误:
org.springframework.beans.factory.UnsatisfiedDependencyException:创建 URL [jar:file:/C:/Users//.m2/repository/org/springframework/data/spring-data-rest 中定义的名为“repositorySearchController”的 bean 时出错-webmvc/3.3.2.RELEASE/spring-data-rest-webmvc-3.3.2.RELEASE.jar!/org/springframework/data/rest/webmvc/RepositorySearchController.class]:通过构造函数参数0表达的不满足的依赖关系;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建类路径资源[org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]中定义的名为“pagedResourcesAssembler”的bean时出错:通过工厂方法实例化Bean失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.web.PagedResourcesAssembler]:工厂方法“pagedResourcesAssembler”抛出异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建类路径资源[org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]中定义的名为“pageableResolver”的bean时出错:通过工厂方法实例化Bean失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver]:工厂方法“pageableResolver”抛出异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建在类路径资源[org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]中定义的名为“sortResolver”的bean时出错:通过工厂方法实例化Bean失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.web.HateoasSortHandlerMethodArgumentResolver]:工厂方法“sortResolver”抛出异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建类路径资源[org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]中定义的名为“repositoryRestConfiguration”的bean时出错:通过工厂方法实例化Bean失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.rest.core.config.RepositoryRestConfiguration]:工厂方法“repositoryRestConfiguration”抛出异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建类路径资源中定义的名称为“repositories”的bean时出错[org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.class]:通过工厂方法实例化Bean失败;嵌套异常是 org.springframework.beans.BeanInstantiationException:无法实例化 [org.springframework.data.repository.support.Repositories]:工厂方法“repositories”抛出异常;嵌套异常是org.springframework.beans.factory.BeanCreationException:创建在JpaRepositoriesRegistrar.EnableJpaRepositoriesConfiguration上声明的@EnableJpaRepositories中定义的com.User.UserRepository中定义的名为“userRepository”的bean时出错:设置bean属性时无法解析对bean“jpaMappingContext”的引用'映射上下文';嵌套异常是org.springframework.beans.factory.BeanCreationException:创建名称为“jpaMappingContext”的bean时出错:调用init方法失败;嵌套异常是 org.hibernate.AnnotationException:没有为实体指定标识符:com.Product.Product
ExcelController
public class ExcelExporter {
private XSSFWorkbook workbook;
private XSSFSheet sheet;
private List<Product> listProducts;
public ExcelExporter(List<Product> listProducts) {
this.listProducts = listProducts;
workbook = new XSSFWorkbook();
sheet = workbook.createSheet("Products");
}
private void writeHeaderRow(){
Row row = sheet.createRow(0);
CellStyle style = workbook.createCellStyle();
XSSFFont font = workbook.createFont();
font.setBold(true);
font.setFontHeight(16);
style.setFont(font);
Cell cell = row.createCell(0);
cell.setCellValue("Product ID");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("Name");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("Kategorie");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("Text");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("Location");
cell.setCellStyle(style);
}
private void writeDataRows(){
int rowCount = 1;
for (Product Products : listProducts) {
Row row = sheet.createRow(rowCount);
Cell cell = row.createCell(0);
cell.setCellValue(Product.getId());
cell = row.createCell(1);
cell.setCellValue(Product.getName());
cell = row.createCell(2);
cell.setCellValue(Product.getCategory());
cell = row.createCell(3);
cell.setCellValue(Product.getText());
cell = row.createCell(4);
cell.setCellValue(Product.getLocation());
}
}
public void export(HttpServletResponse response) throws IOException {
writeHeaderRow();
writeDataRows();
ServletOutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
workbook.close();
outputStream.close();
}
}
Product:
@EnableJpaRepositories
@Entity
@Service
public class Product {
private static Long id;
private static String name;
private static String category;
private static String text;
private static String location;
public Product() {}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public static Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public static String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public static String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public static String getLocation() { return location; }
public void setLocation(String location) {
this.location = location;
}
}
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
<version>2.0</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-security</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.security.oauth.boot</groupId>
<artifactId>spring-security-oauth2-autoconfigure</artifactId>
<version>2.3.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
<version>8.0.22</version>
</dependency>
<dependency>
<groupId>net.sf.supercsv</groupId>
<artifactId>super-csv</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.security</groupId>-->
<!-- <artifactId>spring-security-test</artifactId>-->
<!-- <scope>test</scope>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
我做错了什么?
编辑: 这是更多可能有用的代码:
ProductService
@Service
@Transactional
public class ProductService {
@Autowired
public ProductRepository repo;
public List<Product> listAll() {
return repo.findAll();
}
public void save(Product product) {
repo.save(product);
}
public Product get(long id) {
return repo.findById(id).get();
}
public void delete(long id) {
repo.deleteById(id);
}
}
ProductRepository
public interface ProductRepository extends JpaRepository<Product, Long> {
}
ExcelExporter 应使用
@RestController
标签进行注释。
如果您想使用带有
@Service
标签注释的服务,您应该在控制器类中使用您想要的任何注入或用 @Autowired
注释的服务来注入服务。
由于我的声誉而无法添加评论,而你却没有提及。
如果你想在方法上设置JPA注释,你应该用
@Access(AccessType.PROPERTY)
注释实体类
默认情况下,访问是针对字段的,并且没有字段被定义为产品实体的标识符。因此出现错误:
No identifier specified for entity: com.Product.Product
标题