spring-data-jpa 使用MySQL DB的Spring Data JPA应用程序Postman在任何请求下返回404

e37o9pze  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(263)

我正在尝试使用Spring Data JPA、Spring Web和MySQL构建一个Sping Boot API。我已经做了我应该做的所有事情,它连接到数据库,但是Postman总是返回404,“Not Found”错误,无论我如何尝试。Spring Boot成功初始化:

[0;39m [36mc.cg.TestProject.TestProjectApplication [0;39m [2m:[0;39m Starting TestProjectApplication using Java 17.0.3 on LNAR-5CG1514NF9 with PID 21336 (C:\Users\********\OneDrive -*********\Desktop\Orientation\TestProject\target\classes started by********in C:\Users\********\OneDrive -*********\Desktop\Orientation\TestProject)
[0;39m [36mc.cg.TestProject.TestProjectApplication [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default"
[0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Bootstrapping Spring Data JPA repositories in DEFAULT mode.
[0;39m [36m.s.d.r.c.RepositoryConfigurationDelegate[0;39m [2m:[0;39m Finished Spring Data repository scanning in 80 ms. Found 1 JPA repository interfaces.
[0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat initialized with port(s): 1111 (http)
[0;39m [36mo.apache.catalina.core.StandardService  [0;39m [2m:[0;39m Starting service [Tomcat]
[0;39m [36morg.apache.catalina.core.StandardEngine [0;39m [2m:[0;39m Starting Servlet engine: [Apache Tomcat/9.0.68]
[0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring embedded WebApplicationContext
[0;39m [36mw.s.c.ServletWebServerApplicationContext[0;39m [2m:[0;39m Root WebApplicationContext: initialization completed in 974 ms
[0;39m [36mo.hibernate.jpa.internal.util.LogHelper [0;39m [2m:[0;39m HHH000204: Processing PersistenceUnitInfo [name: default]
[0;39m [36morg.hibernate.Version                   [0;39m [2m:[0;39m HHH000412: Hibernate ORM core version 5.6.12.Final
[0;39m [36mo.hibernate.annotations.common.Version  [0;39m [2m:[0;39m HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
[0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Starting...
[0;39m [36mcom.zaxxer.hikari.HikariDataSource      [0;39m [2m:[0;39m HikariPool-1 - Start completed.
[0;39m [36morg.hibernate.dialect.Dialect           [0;39m [2m:[0;39m HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
[0;39m [36mo.h.e.t.j.p.i.JtaPlatformInitiator      [0;39m [2m:[0;39m HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
[0;39m [36mj.LocalContainerEntityManagerFactoryBean[0;39m [2m:[0;39m Initialized JPA EntityManagerFactory for persistence unit 'default'
[0;39m [36mJpaBaseConfiguration$JpaWebConfiguration[0;39m [2m:[0;39m 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
[0;39m [36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port(s): 1111 (http) with context path ''
[0;39m [36mc.cg.TestProject.TestProjectApplication [0;39m [2m:[0;39m Started TestProjectApplication in 2.839 seconds (JVM running for 3.873)
[0;39m [36mo.a.c.c.C.[Tomcat].[localhost].[/]      [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet'
[0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet'
[0;39m [36mo.s.web.servlet.DispatcherServlet       [0;39m [2m:[0;39m Completed initialization in 1 ms

我的项目分为5个包:主类包(TestProject)、POJO、控制器、服务和仓库。我目前有一个简单的表,我正在使用它来存储用户名和“密码”。这是我的主类,名为TestProjectApplication:

@EntityScan("com.cg.POJOs")
@EnableJpaRepositories("com.cg.repositories")
@SpringBootApplication
public class TestProjectApplication {

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

我的用户POJO和UserRepo存储库:
第一个
这些基本上没有改变,因为它们是相当标准的。更令人困惑的是,我尝试了每一种类型和组合的Sping Boot 注解都没有效果,这是我的UserServices接口,UserServicesImpl实现和UserController。
用户服务和用户服务实现:


# UserServices:

import java.util.List;
public interface UserServices {

    // Save operation
    User saveUser(User user);

    // Read operation
    List<User> fetchUserList();

    // Update operation
    User updateUser(User user, int userId);

    // Delete operation
    void deleteUserById(int userId);
}

# userServicesImpl:

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;

@Service
public class UserServicesImpl implements UserServices{
    @Autowired
    private UserRepo userRepo;

    @Override
    public User saveUser(User user) {
        return userRepo.save(user);
    }

    @Override
    public List<User> fetchUserList() {
        return (List<User>)userRepo.findAll();
    }

...updateUser(User user), deleteUserById(int id) are implemented.

最后是我的控制器类,其中包含一些测试操作:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.*;

@RestController
public class UserController {
    @Autowired 
    private UserServices userServices;

    // Save operation
    @PostMapping("/users")
    public User saveUser(@RequestBody User department)
    {
        return userServices.saveUser(department);
    }

    // Read operation
    @GetMapping("/users")
    public List<User> fetchUserList()
    {
        return userServices.fetchUserList();
    }

    // Read operation
    @GetMapping("/test")
    public List<User> fetchUsers()
    {
        return Arrays.asList(
                new User(12, "Carlos","123123"),
                new User(2, "donald","123456")
        );
    }

我所做的所有请求,只是返回一个404“Not Found”错误。一个示例请求是“GET http://localhost:1111/test”。我可能显示的最后一点信息可能会显示我的错误,是我的application.properties文件。

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql: true
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
server.port = 1111
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect

如果您有任何关于它为什么不工作的想法,我们将不胜感激!在Spring工具套件IDE上使用Sping Boot 2.7.5和Java 11。

zte4gxcn

zte4gxcn1#

尝试声明您的SpringBootApplication实现扩展了SpringBootServletInitializer。

@EntityScan("com.cg.POJOs")
@EnableJpaRepositories("com.cg.repositories")
@SpringBootApplication
public class TestProjectApplication extends 
org.springframework.boot.web.servlet.support.SpringBootServletInitializer{

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

This sort of explains why that works

相关问题