简体   繁体   English

邮递员要求无效-其余APi实施

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

I have a problem to create a POST or a GET request in postman. 我在邮递员中创建POST或GET请求时遇到问题。 I have an controller that suppose to save a new project or to receive all projects from DB. 我有一个控制器,它想保存一个新项目或从DB接收所有项目。 If I try to save a project in a class that implements CommandLineRunner in database it's everything ok. 如果我尝试将项目保存到在数据库中实现CommandLineRunner的类中,则一切正常。 For more extra details I put my code below: 有关更多详细信息,我将代码放在下面:

This is Project class: 这是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;
    }

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

This is my CommandLineRunner 这是我的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);
            }
        }
    }

My output from console is : 我的控制台输出是:

1 project1 201 gate5 23.0 threshold5 101

I try to do this request from Postman: 我尝试从邮递员发出此请求:

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

And I receive the following output : 我收到以下输出:

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

And in DB only in project table the data was save, in gate table, threshold table, variant_gate_relation and variant_threshold_relation didn't save nothing 在DB中,仅保存了项目表中的数据,而在门表,阈值表中,variable_gate_relation和variant_threshold_relation却什么也没有保存

In your CommandLineRunner , you do this before saving the project. 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);

You should replicate that when saving from endpoint as well (the gateRepository.save() and thresholdRepository.save() are relevant here). 从端点保存时,您也应该复制该文件( gateRepository.save()thresholdRepository.save()在此处相关)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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