java - 如何使用新模型/chid Webflux Spring Reactive 更新/放置对象属性
问题描述
我的模型(Hospital)具有(Procedure)的属性。当我第一次使用只是名称、联系人等的 POST 创建 Hospital 时,我想在不同的 PUT 请求中更新 Procedure 属性。
我试过调用 setProcedure 设置器,但是当我这样做时,我一直在我的医院对象的程序属性中得到它,
"procedure": {
"scanAvailable": true,
"prefetch": -1
}
// it should be like so i.e example
"procedure": {
"name": "xray",
"price": 1.0,
"hosp_id": 111..,
"id" : 222...
}
我首先创建(POST)创建医院,然后通过在程序属性中推送或设置程序的通量来更新(PUT)。
{
"name": "Mercy Hospital",
"address": "3663 S Miami Ave",
"phone": "305-854-4400",
"zipcode": "33133",
"city": "Miami",
"state": "FL",
"lat": 25.7400049,
"lng": -80.21352600000002,
"procedure": {
"scanAvailable": true, <---- the response i'm getting
"prefetch": -1
},
"id": "5d539346e440ed05dfd0fe8a"
}
就像我提到的那样,我创建了两个模型,每个模型都有一个构造函数和 getter 和 setter。
医院
@Document // Identifies this class as domain object to be persisted to mongodb
public class Hospital {
private String name;
private String address;
private String phone;
private String zipcode;
private String city;
private String state;
private double lat;
private double lng;
private List<Procedure> procedure;
@Id
private String id;
//
public Hospital() {
}
public Hospital(String name, String address, String phone, String zipcode, String city, String state, double lat, double lng) throws InterruptedException, ApiException, IOException {
this.name = name;
this.phone = phone;
this.address = address;
this.city = city;
this.state = state;
this.zipcode = zipcode;
this.lat = lng;
this.lng = lng;
}
// All the Getters and Setters
程序
@Document
public class Procedure {
private String name;
private double price;
private String hosp_id;
@Id
private String id;
public Procedure() {
}
public Procedure(String name, double price, String hosp_id) {
this.name = name;
this.price = price;
this.hosp_id = hosp_id;
}
// all the Getters and setters Here
}
控制器 :
@RestController // Controller and ResponseBody Annotations together
@RequestMapping("/hospitals/v1/hosp/") // Create a base string that the endpoint is built upon
@CrossOrigin // For DEV Angular and Spring app locally REMOVE FOR PRODUCTION
public class HospitalController {
private final HospitalService hospitalService;
@Autowired
public HospitalController(HospitalService hospitalService) {
this.hospitalService = hospitalService;
}
@GetMapping(path = "{id}", produces =
MediaType.APPLICATION_JSON_UTF8_VALUE)
public Mono<Hospital> getHospital(@PathVariable String id) {
return hospitalService.getHospital(id);
}
@PutMapping(path = "{hosp_id}/services", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Mono<Hospital> createService(@RequestBody List<Procedure> procedureMono, @PathVariable String hosp_id) {
return hospitalService.createService(procedureMono, hosp_id);
}
服务
public Mono<Hospital> getHospital(String id) {
return reactiveMongoOperations.findById(id, Hospital.class);
}
public Mono<Hospital> createService(List<Procedure> procedureMono, String hosp_id) {
return getHospital(hosp_id).doOnNext(hospital -> hospital.setProcedure(procedureMono));
}
解决方案
你得到这个是因为你试图序列化/反序列化 aFlux
或 a Mono
。
你不能返回
public class Hospital {
private Flux<Procedure> procedure;
}
您只能序列化/反序列化具体类型
public class Hospital {
private List<Procedure> procedure;
}
由于您尚未发布完整的控制器,我无法完全理解您想要做什么,但您无法序列化/反序列化 Monos 或 Fluxes。
推荐阅读
- latex - 如何有效地阻止具有字体颜色的文本?
- vba - 原项目太大时如何制作ACCDE文件
- rest - 使用 rest Api 将 solines 添加到 acumatica
- r - 如何在 gganimate 上添加交点?
- python-trio - 如何使用 trio 构建开始和结束同步调用?
- docker - 如何保存 docker 容器?
- list - 如何将项目添加到 Ansible 中的现有字典?
- javascript - 创建一个接受数字数组的函数
- javascript - 从 Bokeh Google Maps 包装器 (fitBounds()) 调用 Javascript 方法
- ios - 如何在 View Swift 顶部添加标签