Spring Boot REST API生成404找不到

时间:2018-07-02 07:07:49

标签: spring spring-mvc spring-boot java-ee postman

我正在尝试执行连接到MySQL(phpmyadmin)数据库的Spring Boot应用程序。问题是当我尝试获取,删除或发布数据时,它会生成此消息。

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

主类:

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

}

除此之外,我的应用程序中还有User,UserController和UserRepository类。 demo_db数据库和用户表已经在phpmyadmin中创建。

请帮助我解决该问题。预先非常感谢。

有关附加错误截图的详细信息。

enter image description here

1 个答案:

答案 0 :(得分:3)

将以下内容添加到您的-3 -2 -1 0 1 2类中:

Main

如果您在其他地方(如第二个模块中)定义了程序包,则需要手动添加它们。