Reputation: 465
{admin": false,
"email": "[email protected]",
"firstName": "Student",
"idGroup": {
"idGroup": 1,
"name": "BlijkbaarGeenGroep",
"teacher": {
"admin": true,
"email": "1",
"firstName": "Example",
"idUser": 1,
"lastName": "User",
"teacher": true
}
}
The folliwing is returned. What I want jackson todo is to ignore teacher. I don't really care about the teacher items for this call. Problem is I don't want to access the GroupEntity because I do need groupinfo sometimes.
package org.hva.folivora.daomodel.user;
import com.owlike.genson.annotation.JsonIgnore;
import com.owlike.genson.annotation.JsonProperty;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonIgnoreType;
import org.hva.folivora.daomodel.global.GroupEntity;
import javax.persistence.*;
import java.io.Serializable;
/**
* @author
*/
@Entity
@Table(name = "Student", schema = "pad_ijburg", catalog = "")
public class StudentEntity extends UserEntity implements Serializable {
private GroupEntity idGroup;
public StudentEntity(String email, String firstName, String lastName, String password, GroupEntity idGroup) {
//A student cannot be an admin or teacher. (False, False)
super(email, firstName, lastName, password, false, false);
this.idGroup = idGroup;
}
public StudentEntity() {
}
//Something like ignore all but idgroup??!
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "idGroup")
public GroupEntity getIdGroup() {
return idGroup;
}
public void setIdGroup(GroupEntity idGroup) {
this.idGroup = idGroup;
}
}
Upvotes: 0
Views: 834
Reputation: 24527
You have multiple options:
Put @JsonIgnoreProperties
on your StudentEntity
class. See: JsonIgnoreProperties (Jackson-annotations 2.7.4 API).
Write a custom JsonSerializer
for StudentEntity
. Here you will have to write down code that constructs each field for the output JSON. See: Jackson - Custom Serializer | Baeldung.
Use a Mixin, which is basically an interface that matches your StudentEntity
. Here you can use @JsonIgnore
on the getTeacher()
method. See: JacksonMixInAnnotations · FasterXML/jackson-docs Wiki · GitHub.
Put a @JsonSerialize
on you idGroup
property within your StudentEntity
class. The annotation takes a class as argument that implements JsonSerializer
. Though you can specify a way to serialize the idGroup
property if (and only if) Jackson serializes an instance of StudentEntity
. (this is more or less just like option 2.
, but much more simple to implement)
Write a DTO class that matches your output format. Then copy field by field from your StudentEntity
object to the StudentEntityDTO
object (which doesn't have a teacher
property) and then let Jackson serialize the DTO object instead of the original StudentEntity
object. That's a lot of code to write and only useful if the output JSON is really much different from the original object.
I'd go for one of the first four options.
Upvotes: 2
Reputation: 1567
Put @JsonIgnore on getter of teacher, it will not come in out put json
Upvotes: 0