我使用springboot和maryadb数据库进行训练。当我测试数据恢复时,我在邮递员中收到这样一条消息:
{
"timestamp": "2022-03-03T13:53:18.609+00:00",
"status": 404,
"error": "Not Found",
"path": "/user/all"
}
。我在复制粘贴中尝试了几个教程,我总是有相同的消息。我也会把控制台中的消息。提前谢谢你的帮助。
控制器
package controller;
import model.Users;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import service.UsersService;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UsersController {
private UsersService usersService;
public UsersController(UsersService usersService) {
this.usersService = usersService;
}
@GetMapping("/all")
public ResponseEntity<List<Users>> getUsers() {
List<Users> users = usersService.getUsers();
return new ResponseEntity<>(users, HttpStatus.OK);
}
}
服务
package service;
import model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import repository.UsersRepository;
import java.util.List;
@Service
public class UsersService {
private UsersRepository usersRepository;
@Autowired
public UsersService(UsersRepository usersRepository) {
this.usersRepository = usersRepository;
}
public List<Users> saveUsers(List<Users>users){
return usersRepository.saveAll(users);
}
public Users saveUsers(Users users){
return usersRepository.save(users);
}
public List<Users> getUsers(){
return usersRepository.findAll();
}
public Users getUserById(int id_user){
return usersRepository.findById(id_user).orElse(null);
}
}
回应的
package repository;
import model.Users;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UsersRepository extends JpaRepository<Users, Integer> {
}
模型
package model;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.*;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "users")
public class Users {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id_user;
@Column(name="lastname")
private String lastname;
@Column(name="email")
private String email;
@Column(name="password")
private String password;
@Column(name="phone")
private String phone;
@Column(name="adress")
private String adress;
@Column(name="city")
private String city;
@Column(name="country")
private String country;
}
应用属性
spring.jpa.hibernate.ddl-auto = update
spring.datasource.url = jdbc:mysql://localhost:3306/database
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.driver-class-name= com.mysql.cj.jdbc.Driver
spring.jpa.show-sql= true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect
安慰
2022-03-03 14:52:57.479 INFO 24188 --- [ main] c.p.database.databaseApplication : No active profile set, falling back to 1 default profile: "default"
2022-03-03 14:52:58.412 INFO 24188 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2022-03-03 14:52:58.435 INFO 24188 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 8 ms. Found 0 JPA repository interfaces.
2022-03-03 14:52:59.190 INFO 24188 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-03-03 14:52:59.204 INFO 24188 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-03-03 14:52:59.204 INFO 24188 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.58]
2022-03-03 14:52:59.370 INFO 24188 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2022-03-03 14:52:59.370 INFO 24188 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1816 ms
2022-03-03 14:52:59.627 INFO 24188 --- [ main] o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
2022-03-03 14:52:59.696 INFO 24188 --- [ main] org.hibernate.Version : HHH000412: Hibernate ORM core version 5.6.5.Final
2022-03-03 14:52:59.892 INFO 24188 --- [ main] o.hibernate.annotations.common.Version : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2022-03-03 14:53:00.046 INFO 24188 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
2022-03-03 14:53:00.252 INFO 24188 --- [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
2022-03-03 14:53:00.272 INFO 24188 --- [ main] org.hibernate.dialect.Dialect : HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2022-03-03 14:53:00.652 INFO 24188 --- [ main] o.h.e.t.j.p.i.JtaPlatformInitiator : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2022-03-03 14:53:00.666 INFO 24188 --- [ main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2022-03-03 14:53:00.722 WARN 24188 --- [ main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2022-03-03 14:53:01.296 INFO 24188 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2022-03-03 14:53:01.317 INFO 24188 --- [ main] c.p.database.databaseApplication : Started dataBaseApplication in 4.589 seconds (JVM running for 5.786)
2022-03-03 14:53:18.493 INFO 24188 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2022-03-03 14:53:18.493 INFO 24188 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2022-03-03 14:53:18.495 INFO 24188 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
控制器类上的每一件事都得到了正确的实现,而且绝对不需要其他层的实现来共享,您可能会使用SpringBootServletializer,在主类中添加@EnableWebMvc
注释并覆盖SpringApplicationBuilder方法,如下所示:
@SpringBootApplication
@EnableWebMvc
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
否则,如果使用SpringApplication来sun项目,请确保使用@SpringBootApplication
注释的主类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
最后一件事是检查pom文件中是否存在spring boot starter web依赖项
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
如果你不使用Spring启动父不要忘记添加适当的版本到web启动器工件。
我在尝试执行此代码时遇到以下错误。 java.lang.IllegalArgumentException:在“com.app.aggregatorappaconfiguration”上找不到ConfigurationProperties注释。在org.springframework.util.assert.NotNull(assert.java:115)~[spring-core-4.3.2.re
首先要说的是,我一直在寻找解决方案,现在我非常绝望。 当由Spring Boot运行时,我无法从html页面访问css文件。 html。文件 应用JAVA 文件夹结构: 我尝试过将文件夹放入文件夹和/或删除addResourcesHandler,通过相对路径引用css和其他一些东西。似乎没有什么能解决这个问题。请告诉我,如果你试图解决这个问题,但没有找到解决方案,这样我就知道,我不会被忽视。
你好,我试图学习野蝇和springboose一个非常简单的应用程序使用eclipse。项目名称是springboo-test。包括主方法类在内的所有类都在同一个包中。 主方法类称为'App',其代码如下: 以下是服务器日志: 11:36:57281信息[org.wildfly.extension.undertow](服务器服务线程池--68)WFLYUT0021:注册的web上下文:'/sprin
我试图使一个API工作在springstart但是当我输入请求:http://localhost:8080/employee/all我得到这个结果: 它是一个经典的服务,包含一个模型、一个服务、一个存储库、一个映射器和一个异常(如果没有员工),使用的数据库是sql中的实体,如下所示 服务: 仓库 模型 制图员 例外 波姆。xml
我试图创建我的第一个Spring后端,简单的应用程序,存储音乐专辑。不幸的是,我创建的endpoint没有一个工作(我使用postman测试了它们,请求返回404,没有找到)。以下是我所有与项目相关的文件/代码: 我所尝试的: 结果: 正在添加应创建数据库的dataSource方法。 @bean@ConfigurationProperties(前缀=“spring.DataSource”)publ