【问题标题】:How to update/PUT an object property with a new model/chid Webflux Spring Reactive如何使用新模型/chid Webflux Spring Reactive 更新/放置对象属性
【发布时间】:2019-12-22 10:07:02
【问题描述】:

我的模型 (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));
}

【问题讨论】:

  • “我已经尝试设置 Procedures 属性,但不断进入我的 Procedures 属性” -- 抱歉,这不是一个有意义的英文句子。请更清楚地说明您遇到了什么问题,以及您为排除故障所做的工作。
  • 感谢您的理解,我修正了语法和问题
  • 如果你已经解决了这个问题,你也可以为它写一个答案。它对其他人有用。
  • 对不起,我的意思是问题的描述,仍在尝试使其正常工作
  • 您确定在您的Hospital 类中导入了正确的Procedure 吗?

标签: java spring mongodb spring-webflux project-reactor


【解决方案1】:

你得到这个是因为你试图序列化/反序列化 FluxMono

你不能退货

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

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

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

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

【讨论】:

  • 我用控制器和服务更新了上面的代码,我会研究序列化/反序列化
猜你喜欢
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
  • 2021-02-25
  • 2016-09-13
  • 1970-01-01
  • 2016-08-08
相关资源
最近更新 更多