2012-03-23 168 views
0

序列化POJO時使用傑克遜的WRAP_ROOT_VALUE序列設置和序列化POJO應用的命名策略,以一個類名,類名被用作串行化JSON根值。與傑克遜

mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true); 

問題 - 如何將命名策略應用於類名?我想在類名轉換成別的東西。

回答

0

我所做的是通過使用@JsonRootName註解喜歡的方式:

@JsonRootName(value = "smsMessageRequest")   
public class TextMessage { 

    private String message; 
    private String address; 
} 

public static String toJson(Object object) throws IOException { 

    ObjectMapper mapper = new ObjectMapper(); 

    mapper.setVisibility(JsonMethod.FIELD, Visibility.ANY); 
    mapper.configure(SerializationConfig.Feature.WRAP_ROOT_VALUE, true); 

    return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(object); 
} 

其產生:

{ 
    "smsMessageRequest" : { 
    "message" : "abc", 
    "address" : "123" 
    } 
}