You can use this approach as is, you just need to change your input:
@SpringBootApplication
public class So45583717Application {
public static void main(String[] args) {
SpringApplication.run(So45583717Application.class, args);
}
@RestController
@RequestMapping("/")
public static class Ctrl {
@PostMapping
public String post(@RequestBody Integer userId) {
return "UserId is: " + userId;
}
}
}
Example:
curl -XPOST 'localhost:8080' -H'Content-Type: application/json' -d'42'
UserId is: 42%
- { "userId": 42 }, -, .
- :
@RestController
@RequestMapping("/")
public static class Ctrl {
private final ObjectMapper mapper = new ObjectMapper();
@PostMapping
public String post(@RequestBody String body) throws IOException {
final JsonNode jsonNode = mapper.readTree(body);
return "UserId is: " + jsonNode.findValue("userId");
}
}
:
curl -XPOST 'localhost:8080' -H'Content-Type: application/json' -d'{"userId": 42}'
UserId is: 42%