I want to return a JSON made of two strings and don\'t know how to implement it. Here is my code:
@PostMapping
public ResponseEntity<> createUser(@Request
Jackson
does not know about org.json.JSONObject
, use JsonNode instead for key/value
pair.
Update after comment:
Could you write me an example how would you use it? I find it a bit confusing
@PostMapping
public ResponseEntity createUser(@RequestBody User user) {
ObjectMapper objectMapper = new ObjectMapper();
Map responseJson = new HashMap<>();
if (userService.userExists(user)) {
responseJson.put("status",
"User with that username already exists.");
JsonNode jsonNode = objectMapper.valueToTree(responseJson);
return new ResponseEntity(jsonNode,
HttpStatus.BAD_REQUEST);
}
responseJson.put("status", "User created.");
JsonNode jsonNode = objectMapper.valueToTree(responseJson);
return new ResponseEntity<>(jsonNode, HttpStatus.CREATED);
}