- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写使用 Jackson
将对象序列化到 File
或从 File
反序列化的代码。
这些对象本质上是多态的。我的界面看起来像
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@JsonSubTypes({
@JsonSubTypes.Type(RemoteDataSource.class),
@JsonSubTypes.Type(FileDataSource.class)
})
public interface DataSource {
}
以及实现
public class FileDataSource implements DataSource {
private String path;
private String delimiter;
private String fileNamePattern;
@JsonCreator
public FileDataSource(@JsonProperty("path") String path,
@JsonProperty("delimiter") String delimiter,
@JsonProperty("fileNamePattern") String fileNamePattern) {
this.path = path;
this.delimiter = delimiter;
this.fileNamePattern = fileNamePattern;
}
...
}
和
public class RemoteDataSource implements DataSource {
private String host;
private int port;
private String userName;
private String password;
@JsonCreator
public RemoteDataSource(@JsonProperty("host") String host,
@JsonProperty("port") int port,
@JsonProperty("userName") String userName,
@JsonProperty("password") String password) {
this.host = host;
this.port = port;
this.userName = userName;
this.password = password;
}
...
}
并在WorkflowConfig
中使用它们
public class WorkflowConfig {
private String name;
private DataSource dataSource;
@JsonCreator
public WorkflowConfig(@JsonProperty("name") String name,
@JsonProperty("dataSource") DataSource dataSource) {
this.name = name;
this.dataSource = dataSource;
}
...
}
然后我编写我的测试,看起来像
@Test
public void testSerializeAndSerializeFileDataSourceFromFile() throws Exception {
File fileDataSource = temporaryFolder.newFile("fileDataSource.json");
DataSource dataSource = new FileDataSource("/tmp/", ",", ".*");
WorkflowConfig config = new WorkflowConfig("fileWorkflowConfig", dataSource);
ObjectMapper mapper = new ObjectMapper();
mapper.writeValue(fileDataSource, config);
assertTrue(fileDataSource.length() > 0);
DataSource readDataSource = mapper.readValue(fileDataSource, DataSource.class);
assertNotNull(readDataSource);
}
它失败了
com.fasterxml.jackson.databind.JsonMappingException: Unexpected token (END_OBJECT), expected FIELD_NAME: missing property '@type' that is to contain type id (for class com.datasources.DataSource)
at [Source: /var/folders/g3/wtbqfj7d5fsbkz1gm74kqy380000gn/T/junit5577276768876379004/fileDataSource.json; line: 1, column: 123]
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:261)
at com.fasterxml.jackson.databind.DeserializationContext.wrongTokenException(DeserializationContext.java:1340)
at com.fasterxml.jackson.databind.DeserializationContext.reportWrongTokenException(DeserializationContext.java:1196)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedUsingDefaultImpl(AsPropertyTypeDeserializer.java:157)
at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:105)
at com.fasterxml.jackson.databind.deser.AbstractDeserializer.deserializeWithType(AbstractDeserializer.java:142)
at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:42)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3789)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2731)
at com.workflow.WorkflowConfigTest.testSerializeAndSerializeFileDataSourceFromFile(WorkflowConfigTest.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:48)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
序列化后,文件中的数据如下所示
{"name":"fileWorkflowConfig","dataSource":{"@type":"FileDataSource","path":"/tmp/","delimiter":",","fileNamePattern":".*"}}
我相信Jackson
需要@type
信息来构造正确的对象。
但从应用程序的角度来看,我不需要这些信息,所以我使用了
@JsonIgnoreProperties(ignoreUnknown = true)
但是我相信我犯了错误,我现在能做什么?
完整的代码库位于 GitHub 。你可以在那里看到失败的测试
最佳答案
测试有错误,您正在序列化 WorkflowConfig 但试图直接反序列化 DataSource。
测试反序列化应该是:
WorkflowConfig workflowConfig= mapper.readValue(fileDataSource, WorkflowConfig.class);
assertNotNull(workflowConfig);
assertNotNull(workflowConfig.getDataSource());
关于java - 意外的 token (END_OBJECT),应为FIELD_NAME : missing property '@type' that is to contain type id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39215706/
大家好,所有rdf/sparql开发人员。这是一个困扰了我一段时间的问题,但是自从发布rdf和sparql规范以来,似乎没人能准确回答这个问题。 为了说明这种情况,RDF定义了几种方法来处理资源的多值
我在我的应用程序中使用 Bootstrap ,现在遇到了一个大问题。问题是 .container 元素在 1360 px 的屏幕上具有 274px 的左右边距,这是相当大的。结果,一切看起来都被挤到了
我在删除Docker容器时遇到问题-当我使用前一个命令时,它不起作用(Docker报告了容器ID,但没有删除它)。后者起作用了。据我所知,Docker语法是相同的: C:\Users\user>doc
std::back_inserter 仅适用于带有 push_back 的容器,因此它不适用于 set 和 map 另一方面,std::inserter 适用于所有容器类型。那么我可以一直使用 std
我正在开发 Spring Boot + Redis 示例。在此示例中,我开发了一些自定义方法,这些方法基于 RoleName 提取详细信息。对于以下方法 userRepository.findByRo
在我的 Swift 应用程序中尝试实现 Google Tag Manager v5 时,我遇到了以下警告,这给我带来了一些麻烦: GoogleTagManager warning: No defaul
安装了新的 Laravel 8 项目并在加载第一个实例时,出现以下错误。这很奇怪,因为我把它放在一边,后来从 Laravel 5.8 -> 6 升级了另一个项目(工作正常),当我去检查网站时遇到了类似
我有以下测试代码,它只创建一个空的 hashmap (containers.map) 并在之后填充它: hashtable = containers.Map('KeyType','char','Va
我对它们之间的差异有一点了解,但是拥有专家意见将是很棒的。 Container-Optimized Google Compute Engine Images Google Container Engi
我会模板化一个函数,以便将它与 vector、set 或任何其他 STL 容器(具有正确的 API...)一起使用 我的函数当前原型(prototype)是: vector> f ( const ve
我正在尝试匹配包含和不包含某些字符串的 Pandas DataFrame 的行。例如: import pandas df = pandas.Series(['ab1', 'ab2', 'b2', 'c
我需要在一个非常庞大的全文索引数据库中找到一些文本,但我不知道在我的查询术语变体中使用什么更好。 我看过一些使用的例子 SELECT Foo.Bar FROM Foo WHERE
Traceback (most recent call last): File "demo.py", line 132, in `result = find_strawberry(image
我正在尝试编写一个函数,其中一列包含一个子字符串并且不包含另一个子字符串。 在下面的示例中,如果我的行包含“某些项目”并且不包含“开销”,我希望我的函数返回 1。 row| example strin
我试图在文本文件中 append 包含给定字符串集的任何行。我创建了一个测试文件,在其中放置了这些字符串之一。我的代码应该将文本文件中包含这些字符串之一的任何行打印在与文本文件中的上一行相同的行上。这
我正在尝试学习如何使用 std.container 中可用的各种容器结构,但我无法理解如何执行以下操作: 1) 如何创建一个空容器?例如,假设我有一个用户定义的类 Foo,并且想要创建一个应该包含 F
$contains: [1, 2] // @> [1, 2] (PG array contains operator) $contained: [1, 2] // <@ [1,
我看到 CSS 中使用了这种“div#container”语法,我想知道它是如何工作的。有人有它的资源吗? 最佳答案 除了作为上面提到的唯一引用之外,ID 还增加了特异性(我强烈建议您阅读这篇文章或一
我有一个生成很多子对象的应用程序,每个子对象都与一些全局应用程序对象一起工作,例如在全局应用程序注册表中注册自己,更新应用程序统计信息等。 应用程序应该如何将访问这些全局对象的能力传递给 child
Here is a Sencha fiddle of my tab panel setup.按钮被动态添加到 vbox 选项卡容器中,该容器是 hbox 布局设置的一部分。选项卡容器的宽度由 flex
我是一名优秀的程序员,十分优秀!