2012-03-23 10 views
0

JacksonのWRAP_ROOT_VALUEシリアル化設定を使用してPOJOをシリアライズする場合、クラス名はシリアル化されたJSONのルート値として使用されます。JacksonでPOJOをシリアル化するときにクラス名に命名規則を適用する

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

質問 - クラス名にどのように命名方法を適用できますか?私はクラス名を別のものに変換したいと思います。

答えて

0

私はそれをやった方法は次のよう@JsonRootName注釈を使用することです:

{ 
    "smsMessageRequest" : { 
    "message" : "abc", 
    "address" : "123" 
    } 
} 
を:得られた

@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); 
} 

関連する問題