springBoot 和 hibernate 404 未找到

发布于 2025-01-11 13:18:13 字数 6856 浏览 0 评论 0原文

我使用 springboot 和 mariadb 数据库进行训练。当我测试数据恢复时,我在邮递员中收到此消息:

{
    "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

I train with springboot and a mariadb database. When I test for data recovery, I get this message in postman:

{
    "timestamp": "2022-03-03T13:53:18.609+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/user/all"
}

. I tried several tutorials in copy paste and I always have the same message. I will also put the message that is in the console. Thank you in advance for your help.

controller

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);
    }
}

Service

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);
    }
}

Respository

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> {

}

Model

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;

}

Application properties

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

Console

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情痴 2025-01-18 13:18:13

控制器类上的所有内容都已正确实现,绝对不需要其他层实现共享,您可能使用 SpringBootServletInitializer,在主类中添加 @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 来启动项目,请确保主类用 @SpringBootApplication 注释,并且类似于

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

    }

}

And最后一件事检查 pom 文件中是否存在 spring boot starter web 依赖项

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

如果不存在使用 Spring Boot Parent 不要忘记将正确的版本添加到 Web Starter 工件中。

Every thing on the controller class is implemented properly and there is definitely no need for other layers implementation to share, you probably use SpringBootServletInitializer, in the main class add the @EnableWebMvc annotation and override SpringApplicationBuilder method as

@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);
    }

}

Otherwise if you use SpringApplication to sun the project make sure the main class annotated with @SpringBootApplication and be some thing like

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

    }

}

And final thing check the presence of spring boot starter web dependency in you pom file

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

If you do not use spring boot parent do not forget to add the proper version to web starter artifact.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文