简体   繁体   English

杰克逊JSON库:如何使用无法访问其具体表示形式的抽象字段实例化类?

[英]Jackson JSON library: how to instantiate a class with abstract fields that can't access its concrete representation?

This is the same questions than : 这与以下问题相同:

Jackson JSON library: how to instantiate a class that contains abstract fields Jackson JSON库:如何实例化包含抽象字段的类

Nevertheless its solution is not possible since my abstract class is in another project than the concrete one. 但是,它的解决方案是不可能的,因为我的抽象类不在具体的项目中,而是在另一个项目中。

Is there a way then ? 有办法吗?

EDIT 编辑

My architecture is as follows: 我的架构如下:

public class UserDTO {

    ...
    private LanguageDTO lang;

}

I send that object user : 我发送该对象用户:

restTemplate.postForObject(this.getHttpCore().trim() + "admin/user/save/1/" + idUser, userEntity, UserDTO.class);

Then I am supposed to receive it in the function : 然后我应该在函数中接收它:

 @RequestMapping(value = "/save/{admin}/{idUser}", method = RequestMethod.POST)
    public String saveUserById(@RequestBody final UserEntity user, @PathVariable Integer idUser, @PathVariable boolean admin)

with UserEntity defined as : 其中UserEntity定义为:

public class UserEntity extends AbstractUserEntity {
    ...

}

public abstract class AbstractUserEntity {

    ...
    private AbstractLanguageEntity lang;
}

I would like to know how I can specify that lang should be instantiate as LanguageEntity whereas abstract classes are in another project. 我想知道如何指定lang应该实例化为LanguageEntity而抽象类在另一个项目中。

This could work assuming you can configure how the object get serialized. 假设您可以配置对象的序列化方式,那么这可以工作。 See the example here . 请参阅此处的示例。 Look under "1.1. Global default typing" to set the defaults to include extra information in your JSON string, basically the concrete Java type that must be used when deserializing. 在“ 1.1。全局默认类型”下查看以设置默认值,以在JSON字符串中包含额外的信息,这基本上是反序列化时必须使用的具体Java类型。

Since it seems you need to do this for your Spring servlet, you would have to pass a Spring message converter as mentioned here 因为它似乎你需要为你的春天的servlet做到这一点,你必须通过一个弹簧消息变换提到这里

Then inside your custom objectMapper, you can do the necessary configuration: 然后在您的自定义objectMapper中,可以进行必要的配置:

public class JSONMapper extends ObjectMapper {    
    public JSONMapper() {
        this.enableDefaultTyping();
    }    
}

You could probably also make it work with Mix-ins, which allow you to add annotations to classes already defined. 您可能还可以使其与Mix-ins一起使用,从而可以将注释添加到已定义的类中。 You can see and example here . 您可以在此处查看示例。 This will also need to be configured inside the objectMapper. 这也将需要在objectMapper内部进行配置。

If you need the same functionality on your client side (REST template), you can pass the object mapper as shown here . 如果你需要在你的客户端(REST模板)相同的功能,你可以通过对象映射器如图所示这里

The easiest way to solve that issue is to add getters et setters in UserEntity but specifying a concrete class : 解决该问题的最简单方法是在UserEntity中添加getters和setters,但指定一个具体的类:

public LanguageEntity getLang() {
   return (LanguageEntity) lang;
}

public void setLang(LanguageEntity language){
   this.lang = language
}

If all that you want to achieve is to note that LanguageEntity is the implementation of AbstractLanguageEntity , you can register this mapping via module: 如果您要实现的全部目的只是要注意LanguageEntityAbstractLanguageEntity的实现,则可以通过模块注册此映射:

SimpleModule myModule = new SimpleModule())
  .addAbstractTypeMapping(AbstractLanguageEntity.class,
    LanguageEntity.class);
ObjectMapper mapper = new ObjectMapper()
   .registerMdoule(myModule);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM