EN
Spring Boot - accept and return JSON / XML in REST controller (consumes and produces)
6 points
To make this POST request work, we need to add:
- consumes = MediaType.APPLICATION_JSON_VALUE
- produces = MediaType.APPLICATION_JSON_VALUE
- @ResponseBody
xxxxxxxxxx
1
path = "/spring-examples/get-user-data", (
2
method = RequestMethod.POST,
3
consumes = MediaType.APPLICATION_JSON_VALUE,
4
produces = MediaType.APPLICATION_JSON_VALUE)
5
6
public UserResponse getUserRequestMapping( UserRequest userRequest) {
7
// our logic
8
}
Second example:
xxxxxxxxxx
1
path = "/spring-examples/get-user-data", (
2
method = RequestMethod.POST,
3
consumes = "application/json",
4
produces = "application/json")
5
6
public UserResponse getUserRequestMapping( UserRequest userRequest) {
7
// our logic
8
}
See also this quesion: