Home > AI > Backend > SpringBoot > Jackson >

@JsonIgnore

Step 1: install dependency

Step 2: code

MyDto.java

@Data
public class MyDto {

    private String stringValue;
    @JsonIgnore
    private int intValue;
    private boolean booleanValue;
}
@Test
    public void givenFieldIsIgnoredDirectly_whenDtoIsSerialized_thenCorrect()
            throws JsonParseException, IOException {

        ObjectMapper mapper = new ObjectMapper();
        MyDto dtoObject = new MyDto();
        String dtoAsString = mapper.writeValueAsString(dtoObject);
        System.out.println(dtoAsString.toString());
        System.out.println(dtoAsString);
        assertThat(dtoAsString, not(containsString("intValue")));
    }

Related posts:

Leave a Reply