Blue Cheetah
Blue Cheetah

Reputation: 3

Handle empty flux body in client request

I am creating a bulk insert rest endpoint in springboot webflux which looks like the following and works as intended except when a client sends a request to insert 0 objects. I understand that that inserting 0 elements is a pretty extreme edge case for bulk inserting, but I would expect it to silently do nothing rather than fail.

package test;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

@SpringBootApplication
@RestController
public class Application {    
    private static final Logger log = LoggerFactory.getLogger(Application.class);


    public static void main(String[] args) {
        // ignore args and just enable debug mode
        SpringApplication.run(Application.class, args);
    }

    @PostMapping("/insert")
    @ResponseBody
    public static Mono<Void> insert(@RequestBody Flux<MyObject> objects) {
        // implementation details not important
        return objects
            .doOnNext(obj -> log.info("inserting: " + obj.value))
            .then();
    }

    public static class MyObject {
        // actual contents not important
        public String value;
    }
}

Running curl -XPOST -H"Content-Type: application/stream+json" localhost:8080/insert will produce the following in the server's log:

2019-12-06 12:30:27.217 DEBUG 18372 --- [ctor-http-nio-7] o.s.w.s.adapter.HttpWebHandlerAdapter    : [137c5b5c] HTTP POST "/insert"
2019-12-06 12:30:27.218 DEBUG 18372 --- [ctor-http-nio-7] s.w.r.r.m.a.RequestMappingHandlerMapping : [137c5b5c] Mapped to test.Application#insert(Flux)
2019-12-06 12:30:27.219 DEBUG 18372 --- [ctor-http-nio-7] .r.m.a.RequestBodyMethodArgumentResolver : [137c5b5c] Content-Type:application/stream+json
2019-12-06 12:30:27.219 DEBUG 18372 --- [ctor-http-nio-7] .r.m.a.RequestBodyMethodArgumentResolver : [137c5b5c] 0..N [test.Application$MyObject]
2019-12-06 12:30:27.221 DEBUG 18372 --- [ctor-http-nio-7] a.w.r.e.AbstractErrorWebExceptionHandler : [137c5b5c] Resolved [ServerWebInputException: 400 BAD_REQUEST "Request body is missing: public static reactor.core.publisher.Mono<java.lang.Void> test.Application.insert(reactor.core.publisher.Flux<test.Application$MyObject>)"] for HTTP POST /insert
2019-12-06 12:30:27.221 DEBUG 18372 --- [ctor-http-nio-7] o.s.http.codec.json.Jackson2JsonEncoder  : [137c5b5c] Encoding [{timestamp=Fri Dec 06 12:30:27 EST 2019, path=/insert, status=400, error=Bad Request, message=Reques (truncated)...]
2019-12-06 12:30:27.222 DEBUG 18372 --- [ctor-http-nio-7] o.s.w.s.adapter.HttpWebHandlerAdapter    : [137c5b5c] Completed 400 BAD_REQUEST

What I dont understand is why it results in BAD_REQUEST when the log clearly says that it is being routed to test.Application#insert(Flux) with 0..N [test.Application$MyObject] as the request body which suggests that a body with 0 elements should be valid.

Upvotes: 0

Views: 814

Answers (1)

Alexey  Usharovski
Alexey Usharovski

Reputation: 1442

May be @RequestBody(required = false)

Upvotes: 1

Related Questions