gpt4 book ai didi

java - 具有通用约束的 JSON Jackson

转载 作者:行者123 更新时间:2023-12-01 16:47:41 24 4
gpt4 key购买 nike

我在 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 上添加 Java 接口(interface)来约束类的类型。

异常:

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 采用类而不是接口(interface)信息,但没有成功: @JsonTypeInfo(use= JsonTypeInfo.Id.NAME, include= JsonTypeInfo.As.WRAPPER_OBJECT)@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")

你有什么想法吗?

谢谢

<小时/>

[更新]

另一个运行良好的用例:

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\"])

最佳答案

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>> {}

关于java - 具有通用约束的 JSON Jackson,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61744638/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com