@JsonIgnore not working in Mix-in when field has @JsonFormat

19 views
Skip to first unread message

cofe

unread,
May 16, 2019, 11:28:38 AM5/16/19
to jackson-user
same as subject.
Jackson version: 2.9.8

Entity
@Getter
@Setter
public class GeneralUser {
    private String name;
    private String password;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:SS")
    private Date createTime;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:SS")
    private Date modifiedTime;
}

MixInClass
public interface IgnoreCreateAndModifiedTime {
    @JsonIgnore
    String getName();
    @JsonIgnore
    Date getModifiedTime();
}

Test
@Test
public void mixTest() throws JsonProcessingException {
    GeneralUser generalUser = new GeneralUser();
    generalUser.setCreateTime(new Date());
    generalUser.setModifiedTime(new Date());
    generalUser.setName("test");
    generalUser.setPassword("123456");
    System.out.println(new ObjectMapper()
            .addMixIn(GeneralUser.class, IgnoreCreateAndModifiedTime.class)
            .writeValueAsString(generalUser));
}

result
{"password":"123456","createTime":"2019-05-16 07:23:353","modifiedTime":"2019-05-16 07:23:353"}

and @JsonIgnoreProperties can work
@JsonIgnoreProperties({"modifiedTime"})

result
{"password":"123456","createTime":"2019-05-16 07:26:122"}




Tatu Saloranta

unread,
May 16, 2019, 11:44:44 AM5/16/19
to jackson-user
What I'd need is full reproduction (with resolved getters, setters) to
show exact usage, after annotation processing has completed. Worth
filing an issue for.
I am not aware of any bugs in mix-in application code itself.

-+ Tatu +-
Message has been deleted

cofe

unread,
May 16, 2019, 11:14:18 PM5/16/19
to jackson-user
public class GeneralUser {
    private String name;
    private String password;
    @JsonFormat(
        pattern = "yyyy-MM-dd HH:mm:SS"
    )
    private Date createTime;
    @JsonFormat(
        pattern = "yyyy-MM-dd HH:mm:SS"
    )
    private Date modifiedTime;

    public GeneralUser() {
    }

    public String getName() {
        return this.name;
    }

    public GeneralUser setName(String name) {
        this.name = name;
        return this;
    }

    public String getPassword() {
        return this.password;
    }

    public GeneralUser setPassword(String password) {
        this.password = password;
        return this;
    }

    public Date getCreateTime() {
        return this.createTime;
    }

    public GeneralUser setCreateTime(Date createTime) {
        this.createTime = createTime;
        return this;
    }

    public Date getModifiedTime() {
        return this.modifiedTime;
    }

    public GeneralUser setModifiedTime(Date modifiedTime) {
        this.modifiedTime = modifiedTime;
        return this;
    }
}
after compile GeneralUser.Class
I think @JsonIgnore should take precedence over @JsonFormat
在 2019年5月16日星期四 UTC+8下午11:44:44,Tatu Saloranta写道:

Tatu Saloranta

unread,
May 21, 2019, 6:56:51 PM5/21/19
to jackson-user
On Thu, May 16, 2019 at 8:12 PM cofe <tl4...@gmail.com> wrote:
>
> public class GeneralUser {
> private String name;
> private String password;
> @JsonFormat(
> pattern = "yyyy-MM-dd HH:mm:SS"
> )
> private Date createTime;
> @JsonFormat(
> pattern = "yyyy-MM-dd HH:mm:SS"
> )
> private Date modifiedTime;
>
> public GeneralUser() {
> }
>
> public String getName() {
> return this.name;
> }
>
> public GeneralUser setName(String name) {
> this.name = name;
> return this;
> }
>
> public String getPassword() {
> return this.password;
> }
>
> public GeneralUser setPassword(String password) {
> this.password = password;
> return this;
> }
>
> public Date getCreateTime() {
> return this.createTime;
> }
>
> public GeneralUser setCreateTime(Date createTime) {
> this.createTime = createTime;
> return this;
> }
>
> public Date getModifiedTime() {
> return this.modifiedTime;
> }
>
> public GeneralUser setModifiedTime(Date modifiedTime) {
> this.modifiedTime = modifiedTime;
> return this;
> }
> }
> after compile GeneralUser.Class
> I think @JsonIgnore should take precedence over @JsonFormat

Ok I see.

The problem here is, I think, that since `@JsonFormat` and
`@JsonIgnore` are on different accessors (one on field, another on
getter), this is considered to be "split" case, in which annotations
only apply on specific accessor.
`@JsonFormat`, on the other hand, is considered to have effect similar
to `@JsonProperty`, i.e. to indicate inclusion.
I am not sure if the behavior can easily be changed as there is legit
use case where this difference is needed, for example to prevent
"wrong" setter or getter from being used.

But you can make your use case work, I think, by changing mix-in class
to associate `@JsonIgnore` with field, not getter or setter. When
applied to same accessor, effect should be for ignoral to win.

-+ Tatu +-


> 在 2019年5月16日星期四 UTC+8下午11:44:44,Tatu Saloranta写道:
>>
> --
> You received this message because you are subscribed to the Google Groups "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
> To post to this group, send email to jackso...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/jackson-user/0669aed6-9e01-49b2-855f-a625d3238bd6%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages