首页 > 解决方案 > 如何使用新模型/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));
}

标签: javaspringmongodbspring-webfluxproject-reactor

解决方案


你得到这个是因为你试图序列化/反序列化 aFlux或 a Mono

你不能返回

public class Hospital {
    private Flux<Procedure> procedure;
}

您只能序列化/反序列化具体类型

public class Hospital {
    private List<Procedure> procedure;
}

由于您尚未发布完整的控制器,我无法完全理解您想要做什么,但您无法序列化/反序列化 Monos 或 Fluxes。


推荐阅读