java - Postman 请求不起作用 - Rest APi 实现

标签 java spring spring-data-jpa postman spring-rest

我在 postman 中创建 POST 或 GET 请求时遇到问题。我有一个 Controller ,它应该保存一个新项目或从数据库接收所有项目。 如果我尝试将项目保存在数据库中实现 CommandLineRunner 的类中,则一切正常。有关更多额外详细信息,我将代码放在下面:

这是项目类:

@Entity
@Table(name = "project")

public class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "proj_id")
    private int projectId;

    @Column(name = "project_name")
    private String projectName;

    @Column(name = "dg_number")
    private int dgNumber;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "variant_gate_relation",
            joinColumns = {@JoinColumn(name = "proj_id")},
            inverseJoinColumns = {@JoinColumn(name = "gate_id")}
    )
    private Set<Gate> gates = new HashSet<>();

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "variant_threshold_relation",
               joinColumns = @JoinColumn(name = "proj_id"),
               inverseJoinColumns = @JoinColumn(name = "threshold_id"))
    private Set<Threshold> thresholds = new HashSet<>();

    public Project() {

    }

    public Project(String projectName, int dgNumber, Set<Gate> gates, Set<Threshold> thresholds){
        this.projectName = projectName;
        this.dgNumber = dgNumber;
        this.gates = gates;
        this.thresholds = thresholds;
    }

Controller

@RestController
@RequestMapping(ProjectController.PROJECT_URL)
public class ProjectController {

    public static final String PROJECT_URL = "/cidashboard/projects";

    @Autowired
    private final ProjectService projectService;

    public ProjectController(ProjectService projectService) {
        this.projectService = projectService;
    }

    @GetMapping
    public List<Project> getAllProjects(){
        return projectService.findAllProjects();
    }

    @GetMapping("/{id}")
    public Project getProjectById(@PathVariable int id) {
        return projectService.findProjectById(id);
    }

    @PostMapping
 //   @Consumes(MediaType.APPLICATION_JSON_VALUE)
    public Project saveProject(@RequestBody Project newProj) {
        return projectService.saveProject(newProj);
    }
}

这是我的 CommandLineRunner

@Component
public class Test implements CommandLineRunner {

        private final ProjectRepository projectRepository;

        private final GateRepository gateRepository;

        private final ThresholdRepository thresholdRepository;

        public Test(ProjectRepository projectRepository, GateRepository gateRepository, ThresholdRepository thresholdRepository) {
            this.projectRepository = projectRepository;
            this.gateRepository = gateRepository;
            this.thresholdRepository = thresholdRepository;
        }

        @Override
        public void run(String... args) throws Exception {
            Gate gate1 = new Gate("gate5", 23);
            gateRepository.save(gate1);

            Threshold threshold1 = new Threshold(101, "threshold5");
            thresholdRepository.save(threshold1);

            Set<Gate> gates = new HashSet<>();
            gates.add(gate1);

            Set<Threshold> thresholds = new HashSet<>();
            thresholds.add(threshold1);

            Project project1 = new Project("project1", 20, gates, thresholds);
            projectRepository.save(project1);

            List<Project> allProjectsFromDatabase = projectRepository.findAll();

            System.out.println("List of all projects from database : ");

            for (Project project : allProjectsFromDatabase) {
                System.out.println(project.toString());
            }

            System.out.println("---------------------------------------------");

            List<Gate> allGatesFromDatabase = gateRepository.findAll();

            for (Gate gate : allGatesFromDatabase) {
                System.out.println(gate);
            }
        }
    }

我的控制台输出是:

1 project1 201 gate5 23.0 threshold5 101

我尝试执行 postman 的此请求:

  {
   "projectName": "project2",
    "dgnumber": 1,
    "gates": {
        "gateType" : "gate2",
        "gateValue" : 13
    },
    "thresholds": {
        "thresholdType" : "threshold2",
        "thresholdValue" : 22
    }
 }

我收到以下输出:

 {
    "projectId": 3,
    "projectName": "project2",
    "dgnumber": 1
}

并且在数据库中仅在项目表中保存了数据,在门表、阈值表、variant_gate_relation和variant_threshold_relation中没有保存任何内容

最佳答案

CommandLineRunner 中,您可以在保存项目之前执行此操作。

        Gate gate1 = new Gate("gate5", 23);
        gateRepository.save(gate1);

        Threshold threshold1 = new Threshold(101, "threshold5");
        thresholdRepository.save(threshold1);

        Set<Gate> gates = new HashSet<>();
        gates.add(gate1);

        Set<Threshold> thresholds = new HashSet<>();
        thresholds.add(threshold1);

您也应该在从端点保存时复制这一点(gateRepository.save()thresholdRepository.save() 与此处相关)。

关于java - Postman 请求不起作用 - Rest APi 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52530940/

相关文章:

java - 找不到符号变量activity_main

java - PropertiesConfiguration 不想重新加载

java - Spring-Data-Rest中时间的数据类型

java - 如何使用 lombok 为实体中嵌入类的成员变量分配默认值?

具有多个值的 Java hashmap

java - 使用 Jersey 和 Jaxb 自定义 Json 输出

java - 为什么我的 DefaultAnnotationHandlerMapping 没有被调用?

javascript - 尝试将静态 javascript 导入 thymeleaf

java - 扩展 `JpaRepository` ,但仅实现子接口(interface)

java - 包 org.springframework.data.repository 不存在 spring boot jpa