【问题标题】:Pretty-Print parameters for Spring + RESTSpring + REST 的漂亮打印参数
【发布时间】:2020-06-19 06:19:30
【问题描述】:

我使用本教程编写代码(https://spring.io/guides/tutorials/rest/),现在我有这个输出:

[{"id":1,"name":"Bilbo Baggins","role":"burglar"},{"id":2,"name":"Frodo Baggins","role":"thief"}]

但在教程中结果不同:

{
  "_embedded": {
    "employeeList": [
      {
        "id": 1,
        "name": "Bilbo Baggins",
        "role": "burglar",
        "_links": {
          "self": {
            "href": "http://localhost:8080/employees/1"
          },
          "employees": {
            "href": "http://localhost:8080/employees"
          }
        }
      },
      {
        "id": 2,
        "name": "Frodo Baggins",
        "role": "thief",
        "_links": {
          "self": {
            "href": "http://localhost:8080/employees/2"
          },
          "employees": {
            "href": "http://localhost:8080/employees"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/employees"
    }
  }
}

我知道,这是因为 JSON,但我该如何改变呢?

版本:Java jdk 12,Intelij Idea 最后更新,Spring MVC 5.2.4;

我的代码:https://github.com/Rogue06/demonica

【问题讨论】:

  • 您在该仓库的哪个位置创建 json 文件?
  • 我没有这样的文件。我最近正在研究 Spring 和 REST,所以我想获得一份关于更改此 REST 消息外观的分步指南。
  • 在那个 repo 中,你在哪里发回或接收 JSON 输出。你是如何显示它的?
  • 如何称呼端点/employees?用你的浏览器?尝试删除浏览器缓存或从隐身窗口调用http://localhost:8081/employees。当我克隆您的存储库时,我的输出看起来与您想要的完全一样
  • “Last version”对未来的读者没有帮助,对现在的读者来说是个麻烦;提供实际版本。这是 Jackson 配置选项。你在使用引导吗?如果是这样,您正在寻找的属性是spring.jackson.serialization.indent_output。

标签: java json spring rest model-view-controller


【解决方案1】:

我必须将以下代码添加到 app.properties:

spring.jackson.serialization.INDENT_OUTPUT=true

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-22
    • 2016-07-07
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 2014-05-19
    • 1970-01-01
    相关资源
    最近更新 更多