How can I get @JsonIgnore to work I have a class. And even if I put the annotation there it has no effect on the output. I am using Jackson.
public class Que
If you are using an implementation of Jackson and its annotations are not working it's probably because you have another dependency of jackson with better precedence. Hence if you want to assure that certain implementation of jackson prevales (IMHO the best choice is the one that you have all classes already annotated with, because probably it came with other dependencies) specify this dependency in the pom of the application module. So if you have in multiple modules all your entities annotated with
import com.fasterxml.jackson.annotate.JsonIgnore; // note: com. instead of org.
Instead of replacing all imports just specify the corresponding dependency in the application pom:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
This will clarify to Spring Boot that this is the implementation you want to use.
The annotation should only be on the 'get' methods. You seem to have @Json... annotations on your private fields.
I faced the same issue while working with spring and hibernate. The reason for this was that in entity class I was using org.codehaus.jackson.JsonIgnore and in spring I was using com.fasterxml.jackson.jaxrs.JsonIgnore. Fix to any one library in both layers and problem will disappear.
I added @Getter(onMethod_=@JsonIgnore) for boolean.
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
public class TestClass {
@JsonIgnore
private String name;
@Getter(onMethod_=@JsonIgnore)
private boolean isValid;
}
I faced this issue in my project and finally I got the solution.
Replace the dependency...
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
Import : com.fasterxml.jackson.annotation.JsonIgnore;
I hope, It will work.
You Need to change your org.codehaus.jackson version. I am currently using 1.1.1 version in which @JsonIgonre is not working. I changed it with 1.9.13 then @JsonIgnore is working fine.
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.1.1</version>
</dependency>
Change to
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>1.9.13</version>
</dependency>
And Java Code is :-
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.codehaus.jackson.annotate.JsonIgnore;
@Entity
@Table(name="users")
public class Users implements Serializable{
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer id;
private String username;
private String firstname;
@JsonIgnore
private String lastname;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}