Advertisement
Guest User

Untitled

a guest
Nov 24th, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. import com.fasterxml.jackson.annotation.JsonSubTypes;
  2. import com.fasterxml.jackson.annotation.JsonTypeInfo;
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.fasterxml.jackson.databind.SerializationFeature;
  6.  
  7. public class Test {
  8.  
  9.     @JsonTypeInfo( use=JsonTypeInfo.Id.CLASS)
  10.     @JsonSubTypes({ @JsonSubTypes.Type(value = ConcreteA.class)})
  11.     public static interface A {
  12.     }
  13.  
  14.     public static class ConcreteA implements A {
  15.     }
  16.  
  17.     @JsonTypeInfo( use=JsonTypeInfo.Id.CLASS)
  18.     @JsonSubTypes({ @JsonSubTypes.Type(value = ConcreteB.class)})
  19.     public static interface B {
  20.     }
  21.  
  22.     public static class ConcreteB implements B {
  23.         A a;
  24.         public A getA() {
  25.             return a=new ConcreteA();
  26.         }
  27.     }
  28.  
  29.     @org.junit.Test
  30.     public void testSerialisation() throws JsonProcessingException {
  31.         ObjectMapper objectMapper = new ObjectMapper();
  32.         objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
  33.         System.out.println(objectMapper.writeValueAsString(new ConcreteB()));
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement