【问题标题】:JSON Jackson with Generic Constraint具有通用约束的 JSON Jackson
【发布时间】:2023-03-24 12:39:01
【问题描述】:

我在 SpringBoot 上下文中使用 JSON Jackson。 我对带有通用类型约束的 Jackson 反序列化有疑问。

这段代码运行良好:


public abstract class AbstractSyncableWriteAndReadResource<T, K> extends AbstractSyncableReadResource<T, K> {

    ...

    @Timed
    @RequestMapping(method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    @Transactional
    public ResponseEntity<List<SynQueuDTO<T>>> syncPushPut(@RequestBody List<SynQueuDTO<T>> entities) {
        ...
    }

}

但是这段代码不起作用: 此代码工作正常:


public abstract class AbstractSyncableWriteAndReadResource<T extends EntitySyncableWrite, K> extends AbstractSyncableReadResource<T, K> {

    ...

    @Timed
    @RequestMapping(method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
    @Transactional
    public ResponseEntity<List<SynQueuDTO<T>>> syncPushPut(@RequestBody List<SynQueuDTO<T>> entities) {
        ...
    }

}

只有一个区别:在 Generic 上为约束 Class 的类型添加 Java 接口。

例外:

Type definition error: [simple type, class xxx.xxx.xxx.EntitySyncableWrite]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `xxx.xxx.xxx.EntitySyncableWrite` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information\n at [Source: (PushbackInputStream); line: 5, column: 26]

我尝试了很多配置来强制 Jackson 使用 Class 而不是 Interface 信息,但没有成功: @JsonTypeInfo(use= JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.WRAPPER_OBJECT)@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") (ko)

你有什么想法吗?

谢谢


[更新]

另一个运行良好的用例:

public abstract class AbstractSyncableWriteAndReadResource<T extends EntitySyncableWrite, K> ... {

public ResponseEntity<List<SyncQueuDTO<T>>> syncPushPut( @RequestBody SyncQueuDTO<T> data) {
...
}

}

但在将 RequestBody 添加为 List 时不起作用:

public abstract class AbstractSyncableWriteAndReadResource<T extends EntitySyncableWrite, K> ... {

public ResponseEntity<List<SyncQueuDTO<T>>> syncPushPut( @RequestBody List<SyncQueuDTO<T>> data) {
...
}

}

例外:

Type definition error: [simple type, class xx.xx.xx.EntitySyncableWrite]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `xx.xx.xx.EntitySyncableWrite` (no Creators, like default construct, exist): abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information\n at [Source: (PushbackInputStream); line: 5, column: 26] (through reference chain: java.util.ArrayList[0]->xx.xx.xx.SyncQueuDTO[\"serverVersion\"])

【问题讨论】:

  • 查看另一个用例

标签: java json spring-boot generics jackson


【解决方案1】:

Map JSON array of objects to @RequestBody List<T> using jackson 的启发下,我解决了这个 Jackson/JVM 限制:

public abstract class AbstractSyncableWriteAndReadResource<T extends EntitySyncableWrite, K> extends AbstractSyncableReadResource<T, K> {
  ...

public ResponseEntity<List<SyncQueuDTO<T>>> syncPushPut(@RequestBody SyncQueuDTOList<T> queuList) {
....
}
}

和 ArrayList 实现:

public class SyncQueuDTOList<T extends EntitySyncableWrite> extends ArrayList<SyncQueuDTO<T>> {}

【讨论】:

    猜你喜欢
    • 2010-11-02
    • 1970-01-01
    • 1970-01-01
    • 2021-08-22
    • 1970-01-01
    • 2023-04-07
    • 2018-05-03
    • 1970-01-01
    相关资源
    最近更新 更多