Apache Camel:无法从START_ARRAY标记反序列化实例

huangapple 未分类评论41阅读模式
标题翻译

Apache Camel: Cannot deserialize instance of out of START_ARRAY token

问题

我通过 HTTP 访问 REST 服务,该服务返回以下 JSON 数据:

[
  {
    "name": "D",
    "surname": "D"
  },
  {
    "name": "T",
    "surname": "T"
  }
]

为了在调用时将其转换为对象,我创建了以下数据结构:

@Data
public class User {
    String name;
    String surname;
}

我尝试按照以下方式处理数据:

public static void addDirectRestRoute(RouteBuilder routeBuilder, String directEndpointName, String url, String a) {
    routeBuilder.from("direct://" + directEndpointName)
        .setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
        .setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
        //.setHeader(Exchange.HTTP_QUERY, simple("?id=1"))
        .to(url)
        .unmarshal().json(JsonLibrary.Jackson, User.class);
        //.unmarshal(new JacksonDataFormat(User.class));
}

但是出现了错误。我尝试了以下数据结构:

@Data
public class User {
    List<U> u;

    @Data
    public class U {
        String name;
        String surname;
    }
}

我仍然遇到了错误。以下是错误的详细信息:

[ERROR] r.s.e.g.i.r.s.ShopImportRoute - 无法从 START_ARRAY 令牌中反序列化 `ru.sportmaster.esm.geogate.integration.domain.dto.User` 的实例
在 [Source: (org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream); line: 1, column: 1]
com.fasterxml.jackson.databind.exc.MismatchedInputException: 无法从 START_ARRAY 令牌中反序列化 `ru.sportmaster.esm.geogate.integration.domain.dto.User` 的实例
在 [Source: (org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream); line: 1, column: 1]
	at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
	at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1139)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1093)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1461)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:185)

你的数据结构有什么问题?

英文翻译

I go over http to the rest service, which returns this json:

[
  {
    &quot;name&quot;:&quot;D&quot;,
    &quot;surname&quot;:&quot;D&quot;
  },
  {
    &quot;name&quot;:&quot;T&quot;,
    &quot;surname&quot;:&quot;T&quot;
  }
]

In order to change it to an object when called, I created this structure:

@Data
public class User {
    String name;
    String surname;
}

I tried to take it like this:

public static void addDirectRestRoute(RouteBuilder routeBuilder, String directEndpointName, String url, String a) {
    routeBuilder.from(&quot;direct://&quot; + directEndpointName)
        .setHeader(Exchange.CONTENT_TYPE, constant(&quot;application/json&quot;))
        .setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
        //.setHeader(Exchange.HTTP_QUERY, simple(&quot;?id=1&quot;))
        .to(url)
        .unmarshal().json(JsonLibrary.Jackson, User.class);
        //.unmarshal(new JacksonDataFormat(User.class));
}

Get an error. I tried this:

@Data
public class User {
    List&lt;U&gt; u;

    @Data
    public class U {
        String name;
        String surname;
    }
}

I still get an error. Here's the error:

[ERROR] r.s.e.g.i.r.s.ShopImportRoute - Cannot deserialize instance of `ru.sportmaster.esm.geogate.integration.domain.dto.User` out of START_ARRAY token
 at [Source: (org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream); line: 1, column: 1]
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `ru.sportmaster.esm.geogate.integration.domain.dto.User` out of START_ARRAY token
 at [Source: (org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream); line: 1, column: 1]
	at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
	at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1139)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1093)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromArray(BeanDeserializerBase.java:1461)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:185)

What is wrong with my data structure?

huangapple
  • 本文由 发表于 2020年5月31日 04:35:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/62108360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定