简体   繁体   中英

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.

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

    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:

@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. demo_db database and user table is already been created in 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:

@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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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