requestmapping & getmapping are printing out object not the way I want it

Viewed 96

When I run my program and open localhost:8080, I get all this on one line:

{"friends":{"bob":{"name":"bob","age":1},"bobby":{"name":"bobby","age":-4}}}

How do I make it have indents like a json object?

{
    "friends": {
        "bob": {
            "name": "bob",
            "age":1
        },
        "bobby": {
            "name": "bobby",
            "age": -4
        }
    }
}

my code is this:

@RestController
@RequestMapping("/")
public class FriendResource {
    @GetMapping
    public @ResponseBody ResponseEntity<Dude> getDude() {
        return new ResponseEntity<>(new Dude())), HttpStatus.OK);
    }
}
2 Answers

If you only want your JSON to look pretty on your browser end then you can use a chrome extension called JSON Formatter. With this on your chrome browse you don't have to modify your backend code.

The best is to use REST client applications like Postman, Insomnia to query your rest endpoints. They will prettify your JSON response by default and they come with other useful features.

By default, Spring Boot uses Jackson when turning objects into json. To get output in the form that you want, enable its indent output feature. To do so, add the following to your application.properties file in your project’s src/main/resources directory:

spring.jackson.serialization.indent-output=true
Related