仅对某些类使用Json根元素

时间:2022-08-23 08:05:50

I'm using dropwizard to create REST API. But I dont understand, how can I configure Jackson to exclude some classes from WRAP_ROOT_VALUE/UNWRAP_ROOT_VALUE features? Right now I get a post request with json body that doesn't include root element name:

我正在使用dropwizard来创建REST API。但我不明白,如何配置Jackson从WRAP_ROOT_VALUE / UNWRAP_ROOT_VALUE功能中排除某些类?现在我收到一个json正文的帖子请求,不包含根元素名称:

{
   "identification": "dummyuser",
   "password":"dummypass"
}

This should map to java class LoginRequest:

这应该映射到java类LoginRequest:

public class LoginRequest {
    public String identidication;
    public String passwrd;
}

I also get requests for some types that include root element name:

我也收到一些包含根元素名称的类型的请求:

{
    "user":{
        "id":12345,
        "name":"John Doe"
    }
}

This should be mapped to:

这应该映射到:

@JsonRootName("user")
public class User {
   ...
}

To get root element working I had to include:

要使root元素工作,我必须包括:

    environment.getObjectMapper().configure(SerializationFeature.WRAP_ROOT_VALUE, true);
environment.getObjectMapper().configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);

but now it applies for all classes. This means that whenever login request comes in, server will throw an error because it expects to see root element name.

但现在它适用于所有课程。这意味着每当登录请求进入时,服务器都会抛出错误,因为它希望看到根元素名称。

1 个解决方案

#1


0  

Use JsonTypeName with JsonTypeInfo instead of JsonRootName:

将JsonTypeName与JsonTypeInfo一起使用而不是JsonRootName:

@JsonTypeName("user")
@JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT,use= JsonTypeInfo.Id.NAME)
public class User {
   ...
}

@JsonTypeName

Annotation used for binding logical name that the annotated class has. Used with JsonTypeInfo (and specifically its JsonTypeInfo.use() property) to establish relationship between type names and types.

用于绑定带注释的类具有的逻辑名称的注释。与JsonTypeInfo(特别是其JsonTypeInfo.use()属性)一起使用以建立类型名称和类型之间的关系。

@JsonTypeInfo

Annotation used for configuring details of if and how type information is used with JSON serialization and deserialization, to preserve information about actual class of Object instances. This is necessarily for polymorphic types, and may also be needed to link abstract declared types and matching concrete implementation.

用于配置if和如何使用类型信息的详细信息的注释与JSON序列化和反序列化一起使用,以保留有关Object实例的实际类的信息。这必须是多态类型,也可能需要链接抽象声明的类型和匹配具体实现。

#1


0  

Use JsonTypeName with JsonTypeInfo instead of JsonRootName:

将JsonTypeName与JsonTypeInfo一起使用而不是JsonRootName:

@JsonTypeName("user")
@JsonTypeInfo(include= JsonTypeInfo.As.WRAPPER_OBJECT,use= JsonTypeInfo.Id.NAME)
public class User {
   ...
}

@JsonTypeName

Annotation used for binding logical name that the annotated class has. Used with JsonTypeInfo (and specifically its JsonTypeInfo.use() property) to establish relationship between type names and types.

用于绑定带注释的类具有的逻辑名称的注释。与JsonTypeInfo(特别是其JsonTypeInfo.use()属性)一起使用以建立类型名称和类型之间的关系。

@JsonTypeInfo

Annotation used for configuring details of if and how type information is used with JSON serialization and deserialization, to preserve information about actual class of Object instances. This is necessarily for polymorphic types, and may also be needed to link abstract declared types and matching concrete implementation.

用于配置if和如何使用类型信息的详细信息的注释与JSON序列化和反序列化一起使用,以保留有关Object实例的实际类的信息。这必须是多态类型,也可能需要链接抽象声明的类型和匹配具体实现。