【问题标题】:How do I avoid extra json from getting records using CrudRepository如何避免额外的 json 使用 CrudRepository 获取记录
【发布时间】:2018-04-04 07:45:27
【问题描述】:

我在 Spring Boot 应用程序中使用 CrudRepository。它工作正常,但我得到了额外的 json 属性_links。我如何跳过那个额外的 json,因为它会影响渲染大型 json 对象。

我收到了这种json 格式的回复;

    {
  "_embedded": {
    "userses": [
      {
        "user_name": "john doe",
        "_links": {
          "self": {
            "href": "http://localhost:8080/userses/47375"
          },
          "users": {
            "href": "http://localhost:8080/userses/47375"
          }
        }
      },
      {
        "user_name": "john1 doe1",
        "_links": {
          "self": {
            "href": "http://localhost:8080/userses/21665"
          },
          "users": {
            "href": "http://localhost:8080/userses/21665"
          }
        }
      },
      {
        "user_name": "remmya1 decruize1",
        "_links": {
          "self": {
            "href": "http://localhost:8080/userses/47876"
          },
          "users": {
            "href": "http://localhost:8080/userses/47876"
          }
        }
      }
}

我如何忽略 json 中的额外部分。

【问题讨论】:

  • 您能否提供更多信息,也许是一些代码?或者你想要什么而不是你得到什么的例子?看How do I ask a good question
  • @SBylemans 我已经用我得到的响应更新了这篇文章作为输出。我想这很好地解释了我的担忧。
  • stackoverflow.com/questions/23264044/… 也许这对您的情况会有所帮助。
  • 您是如何使用CrudRepository 来实现此响应的?你将它映射到什么对象?通常CrudRepository 被用作class MyObjectRepository implements CrudRepository<MyObject,Id> {...},其中 MyObject 是响应映射到的对象。

标签: java spring spring-mvc spring-boot crud


【解决方案1】:

如果您使用的是CrudRepository,您只需调整要映射到的对象。比如你使用CrudRepository的方式可能是这样的:

class UserRepository implements CrudRepository<User,String> {...}

String 是标识符的类(假定为 user_name

User 类可能如下:

class User {
    @Id
    public String userName;
}

返回时,用户将不包含_links 属性。

【讨论】:

    猜你喜欢
    • 2012-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-01
    • 2020-09-09
    • 2016-12-24
    相关资源
    最近更新 更多