我正在使用Spring Data REST Hibernate 5.2.10。这是我的模型:

    @Entity
    public class WorkSession extends AbstractEntity {
    private static final long serialVersionUID = -5058296954283517829L;

    @NotNull(message = "The user is mandatory")
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private User agent;

    @NotNull(message = "The checkpoint is mandatory")
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    private CheckPoint checkPoint;

    @Column(nullable = false, updatable = false)
    private LocalDateTime startDate = LocalDateTime.now();

    private LocalDateTime endDate;

    @Lob
    private String notes;

    public WorkSession() {
    }


和用户:

@Entity
public class User extends AbstractEntity implements UserDetails {
    private static final long serialVersionUID = 5745401123028683585L;
    public static final PasswordEncoder PASSWORD_ENCODER = new BCryptPasswordEncoder();

    @NotNull(message = "The name of the user cannot be blank")
    @Column(nullable = false)
    private String name;

    /** CONTACT INFORMATION **/
    private String landlinePhone;

    private String mobilePhone;

    @NotNull(message = "The username cannot be blank")
    @Column(nullable = false, unique = true)
    private String username;

    @Email(message = "The email address is not valid")
    private String email;

    @JsonIgnore
    private String password;

    @Column(nullable = false)
    private String timeZone = "Europe/Rome";

    @JsonIgnore
    private LocalDateTime lastPasswordResetDate;

    @Column(nullable = false, columnDefinition = "BOOLEAN default true")
    private boolean enabled = true;

    @Type(type = "json")
    @Column(columnDefinition = "json")
    private String[] roles = new String[] {};


在我的一项服务中,我正在创建此查询:

@RestResource(rel = "findActiveWorkSessionForUser", path = "findActiveWorkSessionForUser")
@Query("SELECT ws FROM WorkSession ws JOIN ws.agent u WHERE u.username= ?#{principal.username} AND ws.endDate IS NULL")
@Transactional(readOnly = true)
public List<WorkSession> findByAgentUsernameAndEndDateIsNull();


该存储库通过Spring Data REST公开,当我调用此端点时,结果还包含嵌套对象User,如您在此处看到的:

`{
  "_embedded": {
    "workSessions": [
      {
        "sid": "ed9f01d0-f18d-491b-aad9-4445d457d7eb",
        "createdDate": "2017-06-27T13:13:24",
        "lastModifiedDate": "2017-06-27T13:13:24",
        "lastModifiedBy": "admin",
        "startDate": "2017-06-27T13:13:24",
        "endDate": null,
        "notes": null,
        "new": false,
        "_embedded": {
          "agent": {
            "name": "Administrator",
            "roles": [
              "ROLE_ADMIN"
            ],
            "username": "admin",
            "activeWorkSession": "",
            "_links": {
              "self": {
                "href": "http://localhost:8080/api/v1/users/1{?projection}",
                "templated": true
              }
            }
          }
        },
        "_links": {
          "self": {
            "href": "http://localhost:8080/api/v1/workSessions/1"
          },
          "workSession": {
            "href": "http://localhost:8080/api/v1/workSessions/1"
          },
          "checkPoint": {
            "href": "http://localhost:8080/api/v1/workSessions/1/checkPoint"
          },
          "agent": {
            "href": "http://localhost:8080/api/v1/workSessions/1/agent"
          }
        }
      }
    ]
  },
  "_links": {
    "self": {
      "href": "http://localhost:8080/api/v1/workSessions/search/findActiveWorkSessionForUser"
    }
  }
}`


我想避免这种情况(实际上在我的配置中该字段为LAZY)。有办法避免这种行为吗?

最佳答案

您可以通过在其上添加JsonProperty访问权限来阻止User的JSON序列化。

@JsonProperty(access=WRITE_ONLY)


在此处查看有关此信息的更多信息:https://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonProperty.Access.html

09-27 02:36