java - 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;
}
}
解决方案
控制器
如果您想获得某物的价值,您应该使用 GET 而不是 POST,因为 POST 用于创建或保存某物
你的代码:
@PostMapping("api/v1/patients/{illness}")
public void patientIllness() {
return patService.patientIllness(name, illness)
}
- 缺少参数
patientIllness()
,这就是它无法将值传递给服务的原因 - 返回类型是无效的,所以你仍然无法得到任何东西
建议:
@GetMapping("api/v1/patients/{illness}")
public List<Patient> patientIllness(@PathVariable String illness) {
return patService.patientIllness(illness);
}
- 添加参数
patientIllness(String illness)
并将其与路径参数一起使用,这样它将是patientIllness(@PathVariable String illness)
- 如果您想知道患者列表,返回类型应该是您想知道的,返回应该是
List<Patient>
服务
你的代码:
public void patientIllness(String illness) {
for (int i = 0; i < patientList.size(); i++) {
if (patientList.get(i).equals(illness){
}
}
}
- 返回类型为 void,因此如果您想将某些内容返回给控制器,则可以更改它
- 它在这段代码中磨损了
patientList.get(i).equals(illness)
Type ofpatientList.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;
}
- 返回类型将是
List<Patient>
- 你应该和同类型比较
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());
}
推荐阅读
- node.js - 在 node.js 中创建 Firebase 存储访问令牌
- assembly - 32 位编译器程序集 nasm 中两个 64 位定点数的乘法
- excel - 在活动的 Excel 文档中使用 VBA 从 powershell 连接到 Excel
- sql - 如何根据分组的 A 列获取 B 列的值
- firebase - Firebase Cloud Function 只能由另一个函数触发
- ios - 快速启动屏幕动画后更改为另一个视图控制器
- php - 无法在动态链接库 xampp/php/ext/php_imagic.dll 中定位程序入口点 add_index_zval
- python - 如何键入注释字符串的长度?
- javascript - 用 HTML 的输入替换了 JS 中的固定变量
- reactjs - event.target.value 显示未定义