seamaster
seamaster

Reputation: 391

Unrequired Boolean value from a Spring endpoint always return null

I have a spring api endpoint that looks like this:

 public ResponseEntity<TestObj> getCounterProposals(
      @PathVariable Long testObjId, @RequestBody(required = false) Boolean status)

How do I send a request so that the status parameter gets filled up. My endpoint looks like this:

GET /api/test/{testId}

Right now its value is always null and the testId is populated. I send the request from Postman like this:

enter image description here

Should I wrap the Boolean into some DTO object?

Upvotes: 0

Views: 1445

Answers (2)

Ahmed Sayed
Ahmed Sayed

Reputation: 1554

Since - as other people mentioned - having a body with a GET request is not conventional. If your requirement is to use @GetMapping with a status parameter that should not be visible in the query url, maybe it's also worth taking a look at @RequestHeaders?

For example: @RequestHeader("status") Boolean status

Upvotes: 1

dcalap
dcalap

Reputation: 1072

I have this working:

@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name, @RequestBody Boolean status) {

    System.out.println(status);
    return new Greeting(counter.incrementAndGet(), String.format(template, name));
}

And in Postman invoking the endpoint:

enter image description here

We are passing the @RequestParam name in the url ?name=David and the boolean status in the body.

And is working properly.

Note that the selected tab is Body and the type is JSON. (Right after GraphQL BETA) and the passed value is true. Not status - true as form-data as you were passing it in your Postman request.

Upvotes: 0

Related Questions