Languages
[Edit]
EN

Spring Boot - accept and return JSON / XML in REST controller (consumes and produces)

6 points
Created by:
Indira
319

@RequestMapping example

To make this POST request work, we need to add:

  1. consumes = MediaType.APPLICATION_JSON_VALUE
  2. produces = MediaType.APPLICATION_JSON_VALUE
  3. @ResponseBody
@RequestMapping(path = "/spring-examples/get-user-data", 
        method = RequestMethod.POST,
        consumes = MediaType.APPLICATION_JSON_VALUE,
        produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public UserResponse getUserRequestMapping(@RequestBody UserRequest userRequest) {
    // our logic
}

Second example:

@RequestMapping(path = "/spring-examples/get-user-data", 
        method = RequestMethod.POST,
        consumes = "application/json",
        produces = "application/json")
@ResponseBody
public UserResponse getUserRequestMapping(@RequestBody UserRequest userRequest) {
    // our logic
}

See also this quesion:

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join