当我运行程序并打开 localhost:8080 时,我将所有这些都写在一行上:
{"friends":{"bob":{"name":"bob","age":1},"bobby":{"name":"bobby","age":-4}}}
如何使其像 json 对象一样具有缩进?
{
"friends": {
"bob": {
"name": "bob",
"age":1
},
"bobby": {
"name": "bobby",
"age": -4
}
}
}
我的代码是这样的:
@RestController
@RequestMapping("/")
public class FriendResource {
@GetMapping
public @ResponseBody ResponseEntity<Dude> getDude() {
return new ResponseEntity<>(new Dude())), HttpStatus.OK);
}
}
最佳答案
如果您只想让 JSON 在浏览器端看起来漂亮,那么您可以使用名为 JSON Formatter
的 Chrome 扩展。在 Chrome 浏览器上使用此功能,您无需修改后端代码。
最好是使用 REST 客户端应用程序,例如 Postman , Insomnia查询您的其余端点。默认情况下,它们会美化您的 JSON 响应,并且还具有其他有用的功能。
关于spring-boot - requestmapping 和 getmapping 打印出的对象不是我想要的方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68323859/