从MySQL数据库用户表构建API表,但是测试时,我将获得404状态。任何建议

发布于 2025-01-17 14:56:18 字数 4537 浏览 2 评论 0原文

我试图测试API,但我获得了404状态。有什么建议吗?
测试后: { “时间戳”:“ 2022-03-29T01:50:58.126+00:00”, “状态”:404, “错误”:“找不到”, “路径”:“/api/v1/用户” } 我不确定问题本身是否出现在数据库本身中,还是控制器的代码出现问题?

用户表:

package com.infosys.models;
import com.infosys.role_type.RoleType;
import lombok.Data;
import javax.persistence.*;

@Entity
@Table(name = "users")
@Data
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "username")
    private String userName;

    @Column(name = "password")
    private String password;

    @Column(name = "email" )
    private String email;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @ManyToOne
    @JoinColumn(name = "role_id", nullable = false)
    private RoleType role;


}

角色表

package com.infosys.models;
import com.infosys.role_type.RoleType;
import lombok.Data;
import javax.persistence.*;

@Entity
@Table(name="role")
@Data
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "role_id")
    private RoleType role_type;

}

员工表:

package com.infosys.models;
import com.infosys.role_type.RoleType;
import lombok.Data;
import javax.persistence.*;
import java.time.LocalTime;
import java.util.*;

@Entity
@Table(name="employee")
@Data
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @ManyToOne
    @JoinColumn(name = "role_id", nullable = false)
    private RoleType role;

    @ManyToOne
    @JoinTable(name = "employee_has_manager",
            joinColumns = @JoinColumn(name = "employee_id"),
            inverseJoinColumns = @JoinColumn(name = "manager_id")
    )
    private Set<Manager> manager = new HashSet<>();

    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false)
    private User users;

    @Column(name = "start_time")
    private LocalTime startTime;

    @Column(name = "end_time")
    private LocalTime endTime;

    @Column(name = "total_hrs")
    private Long totalHrs;
}

经理表:

package com.infosys.models;
import com.infosys.role_type.RoleType;
import lombok.Data;
import javax.persistence.*;
import javax.persistence.Table;
import java.util.*;


@Entity
@Table(name="manager")
@Data
public class Manager {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false)
    private User users;

    @ManyToOne
    @JoinColumn(name = "role_id", nullable = false)
    private RoleType role;

    @OneToMany(mappedBy = "manager")
    private Set<Employee> employee = new HashSet<>();

    @Column(name = "employee_review")
    private String employeeReview;

}

用户存储库:

package com.infosys.repositories;
import com.infosys.models.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

用户控制器:

package com.infosys.controllers;
import com.infosys.models.User;
import com.infosys.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

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

    @Autowired
    private UserRepository userRepository;


    // get all users
    @GetMapping("/users")
    public String getAllUsers() {
        return "Test";
    }
}

Spring App:

package com.infosys;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
@ComponentScan("com.infosys.repositories")
public class Application {

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

    }

}

I tried to test the API, but I get a 404 status. Any advice?
After testing:
{
"timestamp": "2022-03-29T01:50:58.126+00:00",
"status": 404,
"error": "Not Found",
"path": "/api/v1/users"
}
I'm not sure if the problem is in the database itself or something went wrong with my code for the controller?

User Table:

package com.infosys.models;
import com.infosys.role_type.RoleType;
import lombok.Data;
import javax.persistence.*;

@Entity
@Table(name = "users")
@Data
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "username")
    private String userName;

    @Column(name = "password")
    private String password;

    @Column(name = "email" )
    private String email;

    @Column(name = "first_name")
    private String firstName;

    @Column(name = "last_name")
    private String lastName;

    @ManyToOne
    @JoinColumn(name = "role_id", nullable = false)
    private RoleType role;


}

Role Table

package com.infosys.models;
import com.infosys.role_type.RoleType;
import lombok.Data;
import javax.persistence.*;

@Entity
@Table(name="role")
@Data
public class Role {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Column(name = "role_id")
    private RoleType role_type;

}

Employee Table:

package com.infosys.models;
import com.infosys.role_type.RoleType;
import lombok.Data;
import javax.persistence.*;
import java.time.LocalTime;
import java.util.*;

@Entity
@Table(name="employee")
@Data
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @ManyToOne
    @JoinColumn(name = "role_id", nullable = false)
    private RoleType role;

    @ManyToOne
    @JoinTable(name = "employee_has_manager",
            joinColumns = @JoinColumn(name = "employee_id"),
            inverseJoinColumns = @JoinColumn(name = "manager_id")
    )
    private Set<Manager> manager = new HashSet<>();

    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false)
    private User users;

    @Column(name = "start_time")
    private LocalTime startTime;

    @Column(name = "end_time")
    private LocalTime endTime;

    @Column(name = "total_hrs")
    private Long totalHrs;
}

Manager Table:

package com.infosys.models;
import com.infosys.role_type.RoleType;
import lombok.Data;
import javax.persistence.*;
import javax.persistence.Table;
import java.util.*;


@Entity
@Table(name="manager")
@Data
public class Manager {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @ManyToOne
    @JoinColumn(name = "user_id", nullable = false)
    private User users;

    @ManyToOne
    @JoinColumn(name = "role_id", nullable = false)
    private RoleType role;

    @OneToMany(mappedBy = "manager")
    private Set<Employee> employee = new HashSet<>();

    @Column(name = "employee_review")
    private String employeeReview;

}

User Repository:

package com.infosys.repositories;
import com.infosys.models.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;


@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}

User Controller:

package com.infosys.controllers;
import com.infosys.models.User;
import com.infosys.repositories.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.*;

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

    @Autowired
    private UserRepository userRepository;


    // get all users
    @GetMapping("/users")
    public String getAllUsers() {
        return "Test";
    }
}

Spring App:

package com.infosys;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;


@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class })
@ComponentScan("com.infosys.repositories")
public class Application {

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

    }

}

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文