0

Given the following class below, how do I get a list of properties as Jackson views it (conforming to @JsonViews)

public class MyDto {

    @JsonView(A.class)
    private int intValue1;

    @JsonView(B.class)
    private int intValue2;

    @JsonView({A.class, B.class})
    private int intValue3;

    // standard setters and getters are not shown
}

I have tried following links with no solution:

Marcus Chiu
  • 65
  • 1
  • 8

1 Answers1

0

You should be setting the writer of ObjectMapper to use appropriate view when serializing:

ObjectMapper mapper = new ObjectMapper();//

MyDto dto = new MyDto(1, 2, 3);

String viewA = mapper.writerWithView(A.class).writeValueAsString(dto);
String viewB = mapper.writerWithView(B.class).writeValueAsString(dto);

System.out.println("viewA: " + viewA);  
System.out.println("viewB: " + viewB);

Output:

viewA: {"intValue1":1,"intValue3":3}
viewB: {"intValue2":2,"intValue3":3}
Nowhere Man
  • 19,170
  • 9
  • 17
  • 42
  • yes this is for serializing to String. But to clarify, Im looking for a List of properties. Something like ``` List properties = beanDescription.findProperties(); ``` – Marcus Chiu Jun 18 '21 at 22:29