Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 789
crooked Avatar asked Dec 24 '22 17:12

crooked


2 Answers

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

like image 160
Zico Avatar answered Dec 26 '22 07:12

Zico


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"

like image 38
so-random-dude Avatar answered Dec 26 '22 05:12

so-random-dude