jyr*_*and 5 java json jackson dropwizard
我正在使用dropwizard来创建REST API.但我不明白,如何配置Jackson从WRAP_ROOT_VALUE/UNWRAP_ROOT_VALUE功能中排除某些类?现在我收到一个json主体的帖子请求,不包含根元素名称:
{
"identification": "dummyuser",
"password":"dummypass"
}
Run Code Online (Sandbox Code Playgroud)
这应该映射到java类LoginRequest:
public class LoginRequest {
public String identidication;
public String passwrd;
}
Run Code Online (Sandbox Code Playgroud)
我也收到一些包含根元素名称的类型的请求:
{
"user":{
"id":12345,
"name":"John Doe"
}
}
Run Code Online (Sandbox Code Playgroud)
这应该映射到:
@JsonRootName("user")
public class User {
...
}
Run Code Online (Sandbox Code Playgroud)
要使root元素工作,我必须包括:
environment.getObjectMapper().configure(SerializationFeature.WRAP_ROOT_VALUE, true);
environment.getObjectMapper().configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
Run Code Online (Sandbox Code Playgroud)
但现在它适用于所有类.这意味着每当登录请求进入时,服务器都会抛出错误,因为它希望看到根元素名称.
使用JsonTypeNamewithJsonTypeInfo代替JsonRootName:
@JsonTypeName("user")
@JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT,use= JsonTypeInfo.Id.NAME)
public class User {
...
}
Run Code Online (Sandbox Code Playgroud)
用于绑定带注释的类所具有的逻辑名称的注释。与 JsonTypeInfo(特别是其 JsonTypeInfo.use() 属性)一起使用来建立类型名称和类型之间的关系。
用于配置类型信息是否以及如何与 JSON 序列化和反序列化一起使用的详细信息的注释,以保留有关对象实例的实际类的信息。这对于多态类型是必要的,并且也可能需要链接抽象声明类型和匹配的具体实现。
| 归档时间: |
|
| 查看次数: |
568 次 |
| 最近记录: |