crooked
crooked

Reputation: 715

Single field body in Spring Rest request

in Spring, is it possible to eg. POST single object to controller with @RequestBody? Something like this:

@RequestMapping(value = "/users", method = RequestMethod.POST)
public ResponseEntity<Void> createUser(@RequestBody Long userId) {
    // do smth with userId
}

If yes, what should json body look like?

Upvotes: 9

Views: 9447

Answers (2)

Nur Zico
Nur Zico

Reputation: 2447

As your @RequestBody is primitive so you have to send just simple number in body. Following snap of request body using POSTMAN request body screenshot

Upvotes: 12

so-random-dude
so-random-dude

Reputation: 16465

It is absolutely possible.

This is a curl command that gets back a Http 200 for the above said end point

curl -v http://localhost:8080/users -X POST --header "Content-Type:application/json" -d "123"

Data is just a string literal "123"

Upvotes: 2

Related Questions