Reputation: 6244
I'm using Spring Data REST, Hibernate 5.2.10. This is my model:
@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() {
}
and the User:
@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[] {};
In one of my service I'm creating this query:
@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();
This Repository is exposed via Spring Data REST, and when I call this endpoint the result contains also the nested object User like you can see here:
`{
"_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"
}
}
}`
I would like to avoid this (infact the field is LAZY in my configuration). Is there a way to avoid this behaviour?
Upvotes: 2
Views: 266
Reputation: 4306
in WorkSession
you can annotate the User
field with @JsonIgnore
@JsonIgnore
@NotNull(message = "The user is mandatory")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
private User agent;
by which the JSON serialization framework will not call the getter of the agent filed which cause the user object to be loaded
Upvotes: 0
Reputation: 146
You can prevent JSON serialization of User by adding JsonProperty access on it.
@JsonProperty(access=WRITE_ONLY)
See more information about it here: https://fasterxml.github.io/jackson-annotations/javadoc/2.6/com/fasterxml/jackson/annotation/JsonProperty.Access.html
Upvotes: 1