首页 > 解决方案 > Spring Boot 患者应用程序

问题描述

出于某种原因的代码图像

我正在尝试运行一些 API。一切运行良好,但我必须做一个补充并且很困惑。当我输入“localhost:8080/api/v1/{illness}”时,我必须在 URL 中包含它,它会显示患有该疾病的人的姓名......帮助。我已经包括了我拥有的两个课程。我没有包括我的病人课。它只是名称、性别、疾病和 id 的构造函数和 getter/setter。我也没有包括主井,因为它不需要

package com.sw409.Patientdemo.controller;

import java.util.List;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.sw409.Patientdemo.model.Patient;
import com.sw409.Patientdemo.service.PatientService;

@RestController
public class PatientController {

    PatientService patService = new PatientService();

    // CREATE
    @PostMapping("api/v1/patients")
    public Patient addPatient(@RequestBody Patient p) {
        return patService.addPatient(p);

    }
    
    //GET ILLNESS (HELP!!!!!!!!!!!!!!!!!!!!!!!)
    @PostMapping("api/v1/patients/{illness}")
    public void patientIllness() {
        return patService.patientIllness(name, illness)
    }
    

    // READ
    @GetMapping("api/v1/patients")
    public List<Patient> getPatient() {
        return patService.getPatients();

    }

    // UPDATE
    @PatchMapping("api/v1/patient/{patid}")
    public void updatePatient(@PathVariable("patid") Integer id, @RequestBody Patient p) {
        patService.updatePatient(id, p);

    }

    // DELETE
    @DeleteMapping("api/v1/patient/{patid}")
    public void deletePatient(@PathVariable("patid") Integer id) {
        patService.deletePatient(id);
    }
}

package com.sw409.Patientdemo.service;

import java.util.*;

import com.sw409.Patientdemo.model.Patient;

public class PatientService {

    List<Patient> patientList = new ArrayList<>();

    // Create
    public Patient addPatient(Patient pat) {

        patientList.add(pat);
        return pat;
    }

    public List<Patient> getPatients() {

        return patientList;
    }

    //(HELPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP)
    public void patientIllness(String illness) {
        for (int i = 0; i < patientList.size(); i++) {
            if (patientList.get(i).equals(illness){
                
            }
        }
    }

    public void updatePatient(Integer id, Patient p) {

        for (int i = 0; i < patientList.size(); i++) {

            if (patientList.get(i).getId().equals(id)) {
                patientList.set(i, p);
            }
        }
    }

    public void deletePatient(Integer id) {
        for (int i = 0; i < patientList.size(); i++) {
            if (patientList.get(i).getId().equals(id)) {
                patientList.remove(i);
            }
        }
    }
}
package com.sw409.Patientdemo.model;

public class Patient {

    String name;
    Integer id;
    String gender;
    String illness;

    public Patient(String name, Integer id, String gender, String illness) {
        super();
        this.name = name;
        this.id = id;
        this.gender = gender;
        this.illness = illness;
    }

    public Patient() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getIllness() {
        return illness;
    }

    public void setIllness(String illness) {
        this.illness = illness;
    }

}

标签: javaspringspring-booturl

解决方案


控制器

如果您想获得某物的价值,您应该使用 GET 而不是 POST,因为 POST 用于创建或保存某物

你的代码:

@PostMapping("api/v1/patients/{illness}")
public void patientIllness() {
    return patService.patientIllness(name, illness)
}
  1. 缺少参数patientIllness(),这就是它无法将值传递给服务的原因
  2. 返回类型是无效的,所以你仍然无法得到任何东西

建议:

@GetMapping("api/v1/patients/{illness}")
public List<Patient> patientIllness(@PathVariable String illness) {
    return patService.patientIllness(illness);
}
  1. 添加参数patientIllness(String illness)并将其与路径参数一起使用,这样它将是patientIllness(@PathVariable String illness)
  2. 如果您想知道患者列表,返回类型应该是您想知道的,返回应该是List<Patient>

服务

你的代码:

public void patientIllness(String illness) {
    for (int i = 0; i < patientList.size(); i++) {
        if (patientList.get(i).equals(illness){            
        }
    }
}
  1. 返回类型为 void,因此如果您想将某些内容返回给控制器,则可以更改它
  2. 它在这段代码中磨损了patientList.get(i).equals(illness)Type of patientList.get(i)is Patient 并将其与 String 进行比较

建议1:

public List<Patient> patientIllness(String illness) {
    List<Patient> result = new ArrayList<>();
    for (int i = 0; i < patientList.size(); i++) {
        if (patientList.get(i).getIllness().equals(illness)) {
            result.add(patientList.get(i));
        }
    }
    return result;
}
  1. 返回类型将是List<Patient>
  2. 你应该和同类型比较patientList.get(i).getIllness().equals(illness)

建议2:您可以更改为每个

public List<Patient> patientIllness(String illness) {
    List<Patient> result = new ArrayList<>();
    for (Patient patient : patientList) {
        if (patient.getIllness().equals(illness)) {
            result.add(patient);
        }
    }
    return result;
}

建议3:可以改成使用steam,过滤,收藏到list

public List<Patient> patientIllness(String illness) {
    return patientList.stream()
            .filter(patient -> patient.getIllness().equals(illness))
            .collect(Collectors.toList());
}

推荐阅读