简体   繁体   English

如何通过Java Spring Webflux发送多个POST请求

[英]How to Send multiple POST request through Java Spring Webflux

I have a json file of an array with objects. 我有一个包含对象的数组的json文件。 I would like to iterate through each object and send a post request with the body inside. 我想遍历每个对象,并在内部发送一个发帖请求。 My issue or question is trying to iterate through the @RequestBody for each object in the array and call the createObj method. 我的问题是尝试遍历数组中每个对象的@RequestBody并调用createObj方法。

I pasting the json into Postman to test it I already have a createObject like so but it works for a Mono not multiple 我将json粘贴到Postman中进行测试,我已经有一个createObject,但是它适用于Mono而不是multiple

I tried iterating it like so 我试图像这样迭代

@PostMapping(path = "multicreate", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Flux<Hospital> createMultipleHospital(@RequestBody Flux<Hospital> hospital) {
   hospital.collectList().flatMap(x -> {
        return hospitalService.createHospital((Mono<Hospital>) x);
    });
       return null;
}
// Should loop and send 2 POST request.
[
  {
      "name": "Miami Cancer Institute",
      "address": "8900 North Kendall Drive",
      "phone": "786-596-2000",
      "zipcode": "33176",
      "city": "Miami",
      "state": "FL"
   },
   {
      "name": "Mercy Hospital",
      "address": "3663 S Miami Ave",
      "phone": "305-854-4400",
      "zipcode": "33133",
      "city": "Miami",
      "state": "FL"
   }
]

Controller 调节器

@PostMapping(path = "", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public Mono<Hospital> createHospital(@RequestBody Mono<Hospital> hospital) {
    System.out.println(hospital);
    return hospitalService.createHospital(hospital);
}

Service/Repository 服务/资源库

@Override
public Mono<Hospital> createHospital(Mono<Hospital> hospitalMono) {
  return hospitalMono.flatMap(hosp -> {
     try {
        return reactiveMongoOperations.save(
           hosp.createLatCord(hosp));
          } catch (InterruptedException | ApiException | IOException e) {
              e.printStackTrace();
          }
            return Mono.just(hosp);
        });
}

Solved it after i just switched method return type and parameter from MONO to FLUX and worked. 我刚刚将方法返回类型和参数从MONO切换为FLUX并工作后解决了。

@Override
    public Flux<Hospital> createHospital(Flux<Hospital> hospitalMono) {
        return hospitalMono.flatMap(hosp -> {
            try {
                return reactiveMongoOperations.save(
                        hosp.createLatCord(hosp));
            } catch (InterruptedException | ApiException | IOException e) {
                e.printStackTrace();
            }
            return Mono.just(hosp);
        });
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 spring Webflux Java 中记录请求正文 - How to log request body in spring Webflux Java Java Spring Webflux WebClient - 使用 HTTP2 发送 http 请求 - Java Spring Webflux WebClient - send http request using HTTP2 如何使用java在REST api中通过post请求将多个json文档发送到marklogic数据库? - How to send multiple json documents into marklogic database through post request in REST api using java? 如何使用 Spring WebFlux WebFilter 结束请求并发送正确的响应? - How to end request and send proper response using Spring WebFlux WebFilter? Spring Webflux webclient问题,尝试发送post请求时没有任何反应 - Issue with Spring Webflux webclient , nothing happens when trying to send post request 通过Java中的DataOutputStream发送多个POST请求 - Send Multiple POST Requests Through a DataOutputStream in Java Java Spring:通过多个参数发出POST请求 - Java Spring: Make POST Request with Multiple parameters 多插入行 PostgreSQL Java Spring Webflux - Multiple insert rows PostgreSQL Java Spring Webflux 如何在spring webflux Reactive websockets中发送列表 - How to Send a list in spring webflux Reactive websockets 如何将 JSON 中 Java 对象的实例类型发送到 Java Spring-Server Post 请求? - How to send the instance type of a Java Object in JSON to Java Spring-Server Post request?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM