我是 Spring Boot 新手,一直在尝试制作简单的 REST Web 应用程序。我按照不同指南中所示的步骤进行操作,但当我尝试运行应用程序时仍然遇到相同的问题。
这是我每次尝试运行代码时看到的输出
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.3.RELEASE)
2018-07-06 10:47:44.768 INFO 11172 --- [ main] App.App : Starting App on DESKTOP-OTCRH25 with PID 11172 (started by EhabArman-Notebook in E:\java workplace\eclipse-workplace\full)
2018-07-06 10:47:44.784 INFO 11172 --- [ main] App.App : No active profile set, falling back to default profiles: default
2018-07-06 10:47:44.925 INFO 11172 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@75c072cb: startup date [Fri Jul 06 10:47:44 EEST 2018]; root of context hierarchy
2018-07-06 10:47:46.785 INFO 11172 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2018-07-06 10:47:46.816 INFO 11172 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2018-07-06 10:47:46.816 INFO 11172 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.31
2018-07-06 10:47:46.840 INFO 11172 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_171\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:/Program Files/Java/jre1.8.0_171/bin/server;C:/Program Files/Java/jre1.8.0_171/bin;C:/Program Files/Java/jre1.8.0_171/lib/amd64;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Java\jdk1.8.0_171\bin;C:\Program Files\Apache Software Foundation\apache-maven-3.5.4\bin;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;C:\Program Files\PostgreSQL\10\lib;C:\Program Files\PostgreSQL\10\bin;C:\Program Files\apache-log4j-2.11.0-bin\log4j-api-2.11.0.jar;C:\Program Files\apache-log4j-2.11.0-bin\log4j-corei-2.11.0.jar;C:\Program Files\Git\cmd;C:\Program Files\MySQL\MySQL Shell 8.0\bin;C:\Program Files\MySQL\MySQL Server 8.0\bin;;C:\Program Files\Microsoft VS Code\bin;C:\Users\EhabArman-Notebook\Desktop;;.]
2018-07-06 10:47:46.985 INFO 11172 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2018-07-06 10:47:46.985 INFO 11172 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 2060 ms
2018-07-06 10:47:47.172 INFO 11172 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/]
2018-07-06 10:47:47.177 INFO 11172 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-07-06 10:47:47.178 INFO 11172 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-07-06 10:47:47.179 INFO 11172 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-07-06 10:47:47.179 INFO 11172 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*]
2018-07-06 10:47:47.221 WARN 11172 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'homePage': Unsatisfied dependency expressed through field 'itemRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'App.components.repository.ItemRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2018-07-06 10:47:47.224 INFO 11172 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2018-07-06 10:47:47.247 INFO 11172 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-07-06 10:47:47.390 ERROR 11172 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field itemRepository in App.components.controller.HomePage required a bean of type 'App.components.repository.ItemRepository' that could not be found.
Action:
Consider defining a bean of type 'App.components.repository.ItemRepository' in your configuration.
这是 pom.xml
4.0.0
Test
Full
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.3.RELEASE
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
org.springframework.data
spring-data-jpa
org.hibernate
hibernate-core
1.8
my classes and hierarchy
应用程序类:
package App;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
package App.components.controller;
import org.springframework.beans.factory.annotation.Autowired;
nhập org.springframework.stereotype.Controller;
nhập org.springframework.web.bind.annotation.RequestMapping;
import App.components.repository.*;
主页类:
import App.components.repository.*;
@Bộ điều khiển
public class HomePage {
@Autowired
private ItemRepository itemRepository;
@RequestMapping("/homepage")
public String getHomePage() {
return "homepage/homepage.html";
}
}
项目存储库
package App.components.repository;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import App.components.model.Item;
@Repository
public interface ItemRepository extends CrudRepository- {
}
项目类别:
package App.components.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
@Thực thể
@Table(name = "item")
public class Item {
@Nhận dạng
@GeneratedValue
@Column(name = "id")
private int id;
@Column(name = "name")
chuỗi tên riêng tư;
@Column(name = "quantity")
private int quantity;
@Column(name = "price")
private int price;
public Item() {}
public Item(String name,int quantity ,int price) {
this.name = tên;
this.quantity = quantity;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = tên;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
công khai String toString() {
return "[" + id + " " + name + " " + price + " " + quantity +"]";
}
}
- 将 spring-data-jpa 更改为 spring-boot-starter-data-jpa 并删除 hibernate 依赖项。它带有启动器。
- 在主类中使用@SpringBootApplication。
Tôi là một lập trình viên xuất sắc, rất giỏi!