【发布时间】:2016-10-19 08:17:26
【问题描述】:
有没有办法将SerializationFeature.WRAP_ROOT_VALUE 的配置作为根元素上的注释而不是使用ObjectMapper?
例如我有:
@JsonRootName(value = "user")
public class UserWithRoot {
public int id;
public String name;
}
使用 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"));
}
结果:
{
"user":{
"id":1,
"name":"John"
}
}
有没有办法将此SerializationFeature 作为注释而不是objectMapper 上的配置?
使用依赖:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.2</version>
</dependency>
【问题讨论】:
-
@assylias yes 也看到了这个答案。但不知道如何将其作为小骆驼案例。需要
user而不是User。不确定这是否可能
标签: java json serialization jackson