简体   繁体   English

Spring Boot REST API生成404找不到

[英]Spring Boot REST API Generates 404 Not Found

I am trying to execute Spring boot application which is connected to MySQL (phpmyadmin) database.The issue is when I try to get, delete or post data, it generates this message. 我正在尝试执行连接到MySQL(phpmyadmin)数据库的Spring Boot应用程序。问题是当我尝试获取,删除或发布数据时,它会生成此消息。

{
  "timestamp": "2018-07-02T06:36:37.000+0000",
  "status": 404,
  "error": "Not Found",
  "message": "No message available",
  "path": "/api/users"
}

Main class: 主班:

public class App extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(App.class);
    }
    public static void main( String[] args )
    {
        System.out.println( "Hello World!" );
        SpringApplication.run(App.class, args);
    }
}

Dependencies: 依赖关系:

<dependencies>
      <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity4</artifactId>
        </dependency>
        <dependency>
            <groupId>com.nulab-inc</groupId>
            <artifactId>zxcvbn</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.nekohtml</groupId>
            <artifactId>nekohtml</artifactId>
            </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.191</version>
        </dependency>
    </dependencies>

Application.properties Application.properties

    spring.datasource.url = jdbc:mysql://localhost:3306/demo_db?useSSL=false
    spring.datasource.username=root
    spring.datasource.password=root
    spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
    spring.jpa.hibernate.ddl-auto=create
    spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.jpa.generate-ddl=true
    spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration

UserController: UserController的:

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    UserRepository userRepoObject;

    @GetMapping("/users")
    public List<User> getAllUsers(){
        return userRepoObject.findAll();
    }

    @PostMapping("/users")
    public User createUser(@Valid @RequestBody User userObj) {
        return userRepoObject.save(userObj);
    }

    @GetMapping("/users/{id}")
    public User getUser(@PathVariable(value = "id") Long userId) {
        return userRepoObject.findById(userId)
                .orElseThrow(() -> new ResourceNotFoundException("User", "id", userId));
    }

    @DeleteMapping("users/{id}")
    public ResponseEntity<?> deleteUser(@PathVariable(value="id") Long userId){
        User user = userRepoObject.findById(userId).orElseThrow(() -> new ResourceNotFoundException("User","id",userId));
        userRepoObject.delete(user);
        return ResponseEntity.ok().build();
    }

}

Apart from that, I have User, UserController, and UserRepository class in my app. 除此之外,我的应用程序中还有User,UserController和UserRepository类。 demo_db database and user table is already been created in phpmyadmin. demo_db数据库和用户表已经在phpmyadmin中创建。

Please help me out with the issue. 请帮我解决这个问题。 Many thanks in advance. 提前谢谢了。

For more information attaching screenshots of errors. 有关更多信息,请附上错误的屏幕截图。

在此处输入图片说明

Add to your Main class the following: 将以下内容添加到您的Main类中:

@SpringBootApplication
@ComponentScan({ "your controllers packages here", "other packages if you use them" })
@EnableJpaRepositories(basePackages = "your repo packages here")
@EntityScan(basePackages = "your entities")

If you have your packages defined else where, like in a second module, you need to manually add them. 如果您在其他位置(如第二个模块中)定义了程序包,则需要手动添加它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM