2017-03-14 1 views
1

나는 봄 부트 프로젝트를 가지고 있고 이런 클래스가 있습니다잭슨 포장을 벗긴/랩은 객체

@Value 
public class A { 
    @JsonUnwrapped 
    OrderKey key; 
    String description; 
    B b; 

    @Value 
    public static class B { 
    String description; 
    } 

} 

@Value 
public class OrderKey { 
    @JsonProperty("_key") 
    String id; 

} 

나는 유지 mixin을 가지고 있지만 간결함이 예에서 주석을 추가합니다. 이것은 JSON에 직렬화 할 때 훌륭하게 작동합니다. 문제는 내가 비 직렬화를 시도 할 때 어쩌면 일부 @JsonWrapped 주석이있는 경우 작동합니다.

간단히 말해서 ArangoDB를 사용하여 문서를 만들거나 만들 수는 있지만 내 값 개체를 사용해야하고 불행히도 키를 String으로 사용할 수 없다면 OrderKey으로 캡슐화됩니다. . @Value 주석은 lombok 프로젝트에서 가져온 것입니다.

이것을 달성 할 방법이 있습니까?

답변

0

믹싱 자체 내에서 serialize/deserialize를 수행하게됩니다.이 경우 키의 @JsonUnwrapped 주석과 다른 믹스 인을 피할 수 있습니다.

믹스 인 :

public class OrderMixin { 

    @JsonDeserialize(using = OrderKeyDeserializer.class) 
    @JsonSerialize(using = OrderKeySerializer.class) 
    @JsonProperty("_key") 
    OrderKey key; 

    @JsonProperty("description") 
    String description; 

    @JsonProperty("amount") 
    String amount; 

    @JsonProperty("operation") 
    Order.Operation operation; 

    @JsonProperty("creationDate") 
    LocalDateTime creationDate; 

    public static class OrderKeySerializer extends JsonSerializer<OrderKey> { 

     public OrderKeySerializer() { 
      super(OrderKey.class); 
     } 

     @Override 
     public void serialize(OrderKey value, JsonGenerator gen, SerializerProvider provider) throws IOException { 
      gen.writeString(value.getOrderId()); 
     } 
    } 

    public static class OrderKeyDeserializer extends JsonDeserializer<OrderKey> { 

     public OrderKeyDeserializer() { 
      super(OrderKey.class); 
     } 

     @Override 
     public OrderKey deserialize(JsonParser jsonParser, DeserializationContext context) throws IOException { 
      JsonNode node = jsonParser.getCodec().readTree(jsonParser); 

      return OrderKey.get(node.asText()); 
     } 
    } 

} 

값 개체 :

@Value 
public class Order implements Serializable { 

    private static final long serialVersionUID = 901109456762331944L; 

    OrderKey key; 

    String description; 

    String amount; 

    Operation operation; 

    LocalDateTime creationDate; 

    @Value 
    public static class Operation { 

    String id; 

    String description; 

    String status; 

    LocalDateTime creationDate; 
    } 

} 


@Value 
public class OrderKey implements Serializable { 

    private static final long serialVersionUID = -8102116676316181864L; 

    private String orderId; 

    public static OrderKey get(String orderId) { 
     return new OrderKey(orderId); 
    } 

} 
0

@JsonCreator으로 표시된 class A에 생성자를 정의 해 볼 수 있습니다. Jackson은이 생성자를 사용하여 A 객체를 만들고 JSON 문서에서 예상하는 필드를 A 필드에 매핑 할 수 있습니다. 간단한 예 : A이 생성자 @Value 암시 @AllArgsConstructor 생성자의 생성을 방지 할 수 있다는

@Value 
public class A { 
    @JsonUnwrapped 
    OrderKey key; 
    String description; 

    @JsonCreator 
    public A(@JsonProperty("key") String key, 
      @JsonProperty("description") String description) { 
     this.key = new OrderKey(key); 
     this.description = description; 
    } 
} 

참고.

Java 8 및 일부 추가 모듈로 생성자 주석을 피할 수도 있습니다. 예를 들어 this 다른 답변을 확인하십시오.