簡體   English   中英

郵遞員要求無效-其余APi實施

[英]Postman request doesn't work - Rest APi implementation

我在郵遞員中創建POST或GET請求時遇到問題。 我有一個控制器,它想保存一個新項目或從DB接收所有項目。 如果我嘗試將項目保存到在數據庫中實現CommandLineRunner的類中,則一切正常。 有關更多詳細信息,我將代碼放在下面:

這是Project類:

@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;
    }

調節器

@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

我嘗試從郵遞員發出此請求:

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

我收到以下輸出:

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

在DB中,僅保存了項目表中的數據,而在門表,閾值表中,variable_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()在此處相關)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM