- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用fasterxml将json回复解析为POJO。但问题是 json 回复包含嵌套对象,其中包含反斜杠,因此在通过 ObjectMapper 读取此值时,我收到 com.fasterxml.jackson.databind.exc.MismatchedInputException
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of
com.am.api.Message
(although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('{"entryType":"Buy","rate":"22000.0","action":"update","offerId":"b96f2da7-55f9-4221-aaa3-8e3ad177567d","market":"BTC-PLN","state":{"market":"BTC-PLN","offerType":"Buy","id":"b96f2da7-55f9-4221-aaa3-8e3ad177567d","currentAmount":"0.0005","lockedAmount":"11.00","rate":"22000.0","startAmount":"0.0005","time":"1535023208260","postOnly":false,"hidden":false,"mode":"limit","receivedAmount":"0"}}') at [Source: (String)"{ "topic":"trading/offers/BTC-PLN", "message":"{\"entryType\":\"Buy\",\"rate\":\"22000.0\",\"action\":\"update\",\"offerId\":\"b96f2da7-55f9-4221-aaa3-8e3ad177567d\",\"market\":\"BTC-PLN\",\"state\":{\"market\":\"BTC-PLN\",\"offerType\":\"Buy\",\"id\":\"b96f2da7-55f9-4221-aaa3-8e3ad177567d\",\"currentAmount\":\"0.0005\",\"lockedAmount\":\"11.00\",\"rate\":\"22000.0\",\"startAmount\":\"0.0005\",\"time\":\"1535023208260\",\"postOnly\":false,\"hidden\":false,\"mode\":\"limit\",\"receivedAmoun"[truncated 45 chars]; line: 3, column: 13] (through reference chain: com.am.api.WsOrderReply["message"]) at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63) at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1329) at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1031) at com.fasterxml.jackson.databind.deser.ValueInstantiator._createFromStringFallbacks(ValueInstantiator.java:370) at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:314) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1351) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:170) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:161) at com.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:519) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeWithErrorWrapping(BeanDeserializer.java:527) at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeUsingPropertyBased(BeanDeserializer.java:416) at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1265) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:325) at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159) at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4001) at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992) at com.am.ReplyMapper.readValue(ReplyMapper.java:154) at com.am.ReplyMapper.mapReplyToCommonExecutionReport(ReplyMapper.java:73) at com.am.ReplyMapper.lambda$apply$1(ReplyMapper.java:54) at java.util.Optional.map(Optional.java:215) at com.am.ReplyMapper.apply(ReplyMapper.java:54) at com.am.ReplyMapperTest.shouldMapUpdateOrderReplyNew(ReplyMapperTest.java:64) 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.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.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26) 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:68) at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
我不知道如何使用fasterxml解析这个对象?!
我的 POJO 对象模型如下所示:
@EqualsAndHashCode
@ToString
@Getter
@Builder
public class WsOrderReply {
private final String topic;
private final Message message;
private final Long timestamp;
@JsonCreator
public WsOrderReply(
@JsonProperty("topic") String topic,
@JsonProperty("message") Message message,
@JsonProperty("timestamp") Long timestamp) {
this.topic = topic;
this.message = message;
this.timestamp = timestamp;
}
}
@EqualsAndHashCode
@ToString
@Getter
public class Message {
private final String entryType;
private final BigDecimal rate;
private final String action;
private final String offerId;
private final String market;
private final State state;
@JsonCreator
public Message(
@JsonProperty("entryType") String entryType,
@JsonProperty("rate") BigDecimal rate,
@JsonProperty("action") String action,
@JsonProperty("offerId") String offerId,
@JsonProperty("market") String market,
@JsonProperty("state") State state) {
this.entryType = entryType;
this.rate = rate;
this.action = action;
this.offerId = offerId;
this.market = market;
this.state = state;
}
}
@EqualsAndHashCode
@ToString
@Getter
public class State {
private final String market;
private final String offerType;
private final String id;
private final BigDecimal currentAmount;
private final BigDecimal lockedAmount;
private final BigDecimal rate;
private final BigDecimal startAmount;
private final String time;
private final boolean postOnly;
private final boolean hidden;
private final String mode;
private final BigDecimal receivedAmount;
public State(
@JsonProperty("market") String market,
@JsonProperty("offerType") String offerType,
@JsonProperty("id") String id,
@JsonProperty("currentAmount") BigDecimal currentAmount,
@JsonProperty("lockedAmount") BigDecimal lockedAmount,
@JsonProperty("rate") BigDecimal rate,
@JsonProperty("startAmount") BigDecimal startAmount,
@JsonProperty("time") String time,
@JsonProperty("postOnly") boolean postOnly,
@JsonProperty("hidden") boolean hidden,
@JsonProperty("mode") String mode,
@JsonProperty("receivedAmount") BigDecimal receivedAmount) {
this.market = market;
this.offerType = offerType;
this.id = id;
this.currentAmount = currentAmount;
this.lockedAmount = lockedAmount;
this.rate = rate;
this.startAmount = startAmount;
this.time = time;
this.postOnly = postOnly;
this.hidden = hidden;
this.mode = mode;
this.receivedAmount = receivedAmount;
}
}
我收到的原始 json 消息:
{ "topic":"交易/报价/BTC-PLN", "message":"{\"entryType\":\"Buy\",\"rate\":\"22000.0\",\"action\":\"update\",\"offerId\":\"b96f2da7-55f9-4221-aaa3-8e3ad177567d\",\"市场\":\"BTC-PLN\",\"状态\":{\"市场\":\"BTC-PLN\",\"offerType\":\"购买\",\"id\":\"b96f2da7-55f9-4221-aaa3-8e3ad177567d\",\"当前金额\":\"0.0005\",\"锁定金额\":\"11.00\",\"费率\":\"22000.0\",\"startAmount\":\"0.0005\",\"时间\":\"1535023208260\",\"postOnly\":false,\"隐藏\":false,\"模式\":\"限制\",\"收到金额\":\"0\"}}", “时间戳”:1535023208264}
我的 JUnit 测试:
public class ReplyMapperTest {
private static ObjectMapper objectMapper;
private MessageFactory msgFactory = new quickfix.fix42.MessageFactory();
private TypeSelector fixMsgBuilder = FixMessageBuilder
.usingFactory(msgFactory::create)
.withBeginString(FixVersions.BEGINSTRING_FIX42);
private ReplyMapper replyMapper = new ReplyMapper(objectMapper, fixMsgBuilder);
@BeforeClass
public static void beforeClass() {
objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
@Test
public void shouldMapUpdateOrderReplyNew() throws FieldNotFound, IOException {
String json = IOUtils.toString(this.getClass().getResourceAsStream("/json/UpdateOrderReplyNew.json"), StandardCharsets.UTF_8);
//When
List<Message> result = replyMapper.apply(json);
//Then
assertThat(result.get(0).getHeader().getString(MsgType.FIELD), is(Heartbeat.MSGTYPE));
}
也许有人也遇到了和我一样的问题。如何解决这个问题?
最佳答案
即使未转义,您使用的 JSON 也不正确因此它将整个对象视为一个字符串。使用正确的转义 json 可以解决问题,我已经更正了 JSON,让您现在看看它是否有效
{\r\n\t\"topic\": \"trading\/offers\/BTC-PLN\",\r\n\t\"message\": {\r\n\t\t\"entryType\": \"Buy\",\r\n\t\t\"rate\": \"22000.0\",\r\n\t\t\"action\": \"update\",\r\n\t\t\"offerId\": \"b96f2da7-55f9-4221-aaa3-8e3ad177567d\",\r\n\t\t\"market\": \"BTC-PLN\",\r\n\t\t\"state\": {\r\n\t\t\t\"market\": \"BTC-PLN\",\r\n\t\t\t\"offerType\": \"Buy\",\r\n\t\t\t\"id\": \"b96f2da7-55f9-4221-aaa3-8e3ad177567d\",\r\n\t\t\t\"currentAmount\": \"0.0005\",\r\n\t\t\t\"lockedAmount\": \"11.00\",\r\n\t\t\t\"rate\": \"22000.0\",\r\n\t\t\t\"startAmount\": \"0.0005\",\r\n\t\t\t\"time\": \"1535023208260\",\r\n\t\t\t\"postOnly\": false,\r\n\t\t\t\"hidden\": false,\r\n\t\t\t\"mode\": \"limit\",\r\n\t\t\t\"receivedAmount\": \"0\"\r\n\t\t}\r\n\t},\r\n\t\"timestamp\": 1535023208264\r\n}
如果 Json 无法更改,则需要在构造函数中以 String 值的形式获取消息,然后使用对象映射器将其显式转换为对象
public class WsOrderReply {
private final String topic;
private final Message message;
private final Long timestamp;
ObjectMapper mapper = new ObjectMapper();
@JsonCreator
public WsOrderReply(
@JsonProperty("topic") String topic,
@JsonProperty("message") String messageString,
@JsonProperty("timestamp") Long timestamp) {
this.topic = topic;
this.message = mapper.readValue(messageString, Message.class);;
this.timestamp = timestamp;
}
}
另一个不需要更改模型类的解决方案是从服务器获取 JSON,然后在本地将其更改为第一种方法中提到的格式
关于java - 使用fasterxml将json回复解析为对象时出现com.fasterxml.jackson.databind.exc.MismatchedInputException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52021841/
我找不到关于 jackson 的ObjectMapper与其他映射器(例如dozer/mapStruct/modelMapping/etc)之间的区别的任何解释。所有文章都比较了dozer/mapSt
我正在使用Jackson来反序列化Kotlin数据类。我正在使用jackson-kotlin-module,但 jackson 却给我以下错误: Can not construct instance
我正在尝试将包从“com.fasterxml.jackson”重新定位到我自己的包(即“mypackage.com.fasterxml.jackson”),然后在我拥有的另一个 JAR 中使用它。 我
对于JSON对象,Subject: { "permissions":["foo", "bar"], ... } ...我想反序列化为: class Subject { priv
我正在使用 @JsonTypeInfo 和 @JsonSubTypes 来映射基于给定属性的解析子类。这是我想要解析的示例 JSON 的一个人为示例。 { "animals": [ { "
我们正在使用 dropwizard 版本 0.6.3。当我们尝试升级 0.7.0 版本时,我们在服务启动时收到此错误。 线程“main”中的异常 java.lang.VerifyError: clas
我正在尝试实现自定义解串器。因为我只想向默认反序列化器添加功能,所以我尝试在我的自定义反序列化器中存储默认反序列化器:我想使用默认反序列化 json,然后添加其他信息。 我正在尝试使用 BeanDes
我有一个这样的类(class): public class Person { private String name; public String getName(){ return
我有以下 Kotlin 数据类: data class TestObject( val boolField: Boolean, val stringField: Str
使用 Jackson 库,在 Eclipse 4.9.0 版本中出现以下错误 缺少工件 com.fasterxml.jackson.core:jackson-databind:bundle:2.9.6
我试图在我的应用程序中从azure实现keyvault,在为DefaultAzureCredentialBuilder()实现azure-identity:1.5.4 lib后,它会抛出链接错误,如下
我试图在我的应用程序中从azure实现keyvault,在为DefaultAzureCredentialBuilder()实现azure-identity:1.5.4 lib后,它会抛出链接错误,如下
我知道我们可以使用 Jackson MixIn 来重命名属性或忽略属性(参见示例 here )。但是可以添加属性吗? 添加的属性可以是: 一个常数(如版本号) 计算值(例如,如果源类具有 getWid
我有一个在 Wildfly 10 上运行的应用程序,它需要更新版本的 jackson。简单地更新 maven 依赖是行不通的。 Wildflys 自己的版本似乎干扰了... 有人有提示吗? 最佳答案
我在 Tomcat 休息应用程序中运行 Jersey 2.5.1 & Jackson。对于我最初将 POJO 简单地转换为 JSON 的用例,基本设置效果很好。集合很好地转换为 json 数组,如下所
有没有办法告诉 jackson 在序列化过程中忽略那些用非 jackson 注释注释的字段? 例如: @SomeAnnotation private String foo; 我知道有 jackson
我遇到了 jackson 序列化问题, map 中存在空值。显然,这是 Wildfly 9 使用的 Jackson 版本中的一个已知错误 ( https://issues.jboss.org/brow
给定一个像这样的 JSON 映射类: public class Person { @JsonProperty String getName() { ... } @JsonPro
如何让 Jackson 的 XMLMapper 在反序列化时读取根 xml 元素的名称? 我正在将输入 XML 反序列化为通用 Java 类、LinkedHashMap,然后反序列化为 JSON。我想
我对抽象类和 JSON 序列化和反序列化的对象引用有问题。抽象的问题如下所示: 我有一个由节点和边组成的图。每条边连接两个节点。节点可以是红色和绿色的。因此,有一个抽象类Node和两个派生类 RedN
我是一名优秀的程序员,十分优秀!