Gibt es eine Möglichkeit, die Konfiguration SerializationFeature.WRAP_ROOT_VALUE
als Annotation auf dem Stammelement anstatt ObjectMapper
zu verwenden?SerializationFeature.WRAP_ROOT_VALUE als Annotation in jackson json
Zum Beispiel habe ich:
@JsonRootName(value = "user")
public class UserWithRoot {
public int id;
public String name;
}
Mit ObjectMapper:
@Test
public void whenSerializingUsingJsonRootName_thenCorrect()
throws JsonProcessingException {
UserWithRoot user = new User(1, "John");
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
String result = mapper.writeValueAsString(user);
assertThat(result, containsString("John"));
assertThat(result, containsString("user"));
}
Ergebnis:
{
"user":{
"id":1,
"name":"John"
}
}
Gibt es eine Möglichkeit, diese SerializationFeature
als Anmerkung haben und nicht als eine Konfiguration auf der objectMapper
?
Mit Abhängigkeit:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.2</version>
</dependency>
Vielleicht: http://stackoverflow.com/a/31158706/829571 Siehe auch: https://github.com/FasterXML/jackson-annotations/issues/33 – assylias
@ assylias hat diese Antwort auch gesehen. Aber wissen Sie nicht, wie man es als niedrigerer Kamelfall bekommt. Brauche 'Benutzer' und nicht' Benutzer'. Nicht sicher, ob das möglich ist – Patrick