2014-01-17 123 views

回答

2

不是直接。您可以使用@JsonView或JSON過濾器功能來實現相似的包含/排除。

+0

嗨,你能否提供一些例子或者直接給我一些文檔?謝謝。 – Niranjan

+0

Google是你的朋友;你可以試試這個:http://www.cowtowncoder.com/blog/archives/2011/02/entry_443.html或這個http://wiki.fasterxml.com/JacksonFeatureJsonFilter – StaxMan

5

Jackson Model Versioning Module增加了版本控制支持,它滿足GSON的@Since和@Until超集。


比方說你有一個GSON標註型號:

public class Car { 
    public String model; 
    public int year; 
    @Until(1) public String new; 
    @Since(2) public boolean used; 
} 

使用的模塊,你可以把它轉換成以下傑克遜類級別的註解......

@JsonVersionedModel(currentVersion = '3', toCurrentConverterClass = ToCurrentCarConverter) 
public class Car { 
    public String model; 
    public int year; 
    public boolean used; 
} 

...並寫入到當前版本的轉換器:

public class ToCurrentCarConverter implements VersionedModelConverter { 
    @Override 
    public ObjectNode convert(ObjectNode modelData, String modelVersion, 
           String targetModelVersion, JsonNodeFactory nodeFactory) { 

     // model version is an int 
     int version = Integer.parse(modelVersion); 

     // version 1 had a 'new' text field instead of a boolean 'used' field 
     if(version <= 1) 
      modelData.put("used", !Boolean.parseBoolean(modelData.remove("new").asText())); 
    } 
} 

現在只需將模塊配置爲Jackson ObjectMapper並對其進行測試即可。

ObjectMapper mapper = new ObjectMapper().registerModule(new VersioningModule()); 

// version 1 JSON -> POJO 
Car hondaCivic = mapper.readValue(
    "{\"model\": \"honda:civic\", \"year\": 2016, \"new\": \"true\", \"modelVersion\": \"1\"}", 
    Car.class 
) 

// POJO -> version 2 JSON 
System.out.println(mapper.writeValueAsString(hondaCivic)) 
// prints '{"model": "honda:civic", "year": 2016, "used": false, "modelVersion": "2"}' 

免責聲明:我是這個模塊的作者。有關更多功能的更多示例,請參閱GitHub項目頁面。我還編寫了使用該模塊的Spring MVC ResponseBodyAdvise