Camel 异常错误:无法从数组值(标记'JsonTok')反序列化'com.company.FruitsList'类型的值

uqjltbpv  于 2022-11-07  发布在  Apache
关注(0)|答案(1)|浏览(237)

我刚开始用java编码,所以这个问题可能听起来很傻。

**目标:**我正在尝试构建一个Quarkus Rest应用程序,该应用程序具有使用Apache Camel路由的POST方法。目标是将水果数组作为输入,添加附加信息并将其返回给调用应用程序

  • 通过POSTMAN传递到Quarkus应用程序http://localhost:8080/simple的输入 *
[{
    "id": 1,
    "name": "one"
},{
    "id": 2,
    "name": "second"
}]

**问题:**每当我尝试通过HTTP客户端(POSTMAN)调用REST端点时,我都会收到错误。堆栈跟踪如下

有人能帮我找出我做错了什么吗?

{
  "details": "Error id 0fd95541-edf3-443a-9e56-261400017f01-15, com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `com.company.FruitsList` from Array value (token `JsonToken.START_ARRAY`)",
  "stack": "com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `com.company.FruitsList` from Array value (token `JsonToken.START_ARRAY`)\n at [Source: (ByteArrayInputStream); line: 1, column: 1]\r\n\tat com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)\r\n\tat com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1741)\r\n\tat com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1515)\r\n\tat com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1462)\r\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeFromArray(BeanDeserializer.java:638)\r\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:210)\r\n\tat com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:186)\r\n\tat com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)\r\n\tat com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2051)\r\n\tat com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1427)\r\n\tat com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1442)\r\n\tat org.apache.camel.component.jackson.AbstractJacksonDataFormat.unmarshal(AbstractJacksonDataFormat.java:188)\r\n\tat org.apache.camel.support.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:64)\r\n\tat org.apache.camel.impl.engine.DefaultAsyncProcessorAwaitManager.process(DefaultAsyncProcessorAwaitManager.java:83)\r\n\tat org.apache.camel.support.AsyncProcessorSupport.process(AsyncProcessorSupport.java:41)\r\n\tat org.apache.camel.processor.RestBindingAdvice.unmarshal(RestBindingAdvice.java:307)\r\n\tat org.apache.camel.processor.RestBindingAdvice.before(RestBindingAdvice.java:142)\r\n\tat org.apache.camel.processor.RestBindingAdvice.before(RestBindingAdvice.java:51)\r\n\tat org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:341)\r\n\tat org.apache.camel.component.platform.http.vertx.VertxPlatformHttpConsumer.lambda$handleRequest$2(VertxPlatformHttpConsumer.java:185)\r\n\tat io.vertx.core.impl.ContextImpl.lambda$null$0(ContextImpl.java:159)\r\n\tat io.vertx.core.impl.AbstractContext.dispatch(AbstractContext.java:100)\r\n\tat io.vertx.core.impl.ContextImpl.lambda$executeBlocking$1(ContextImpl.java:157)\r\n\tat org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)\r\n\tat org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)\r\n\tat org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)\r\n\tat org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)\r\n\tat org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)\r\n\tat io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)\r\n\tat java.base/java.lang.Thread.run(Thread.java:830)"

这是我的路线代码:

package com.company;

import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;

@ApplicationScoped
public class RouteBuild extends RouteBuilder {

  Logger logger = LoggerFactory.getLogger(RouteBuild.class);

  @Override
  public void configure() throws Exception {

    restConfiguration().component("platform-http").bindingMode(RestBindingMode.auto);

    rest("/simple")
      .post()
      .type(FruitsList.class)
      .outType(AddressRequest.class)
      .route()
      .routeId("New Route")
      .log(LoggingLevel.INFO, "Passing input data to Direct Component for further actions")
      .to("direct:directDataEntry");

    from("direct:directDataEntry")
      .unmarshal().json()
      .log(LoggingLevel.INFO, "Inside the route to validate the data structure of the input")
      .log(LoggingLevel.INFO, "data of input is ${body}")
      .to("direct:directDataEntry1");

    from("direct:directDataEntry1")
      .routeId("validateData")
      //.log(LoggingLevel.INFO,"Passing input data to validate in route \"validationData\"")
      .process(new Processor() {
        @Override
        public void process(Exchange exchange) throws Exception {

          AddressRequest addressRequest = new AddressRequest();
          addressRequest.setCity("Atlanta");
          addressRequest.setInput_id("1");
          addressRequest.setState("GA");
          addressRequest.setStreet(exchange.getIn().getBody(FruitsList.class).getFruitsList().get(0).getName());
          exchange.getIn().setBody(addressRequest);
   }
      });

  }
}

下面是FruitsList、Fruits和AddressRequest类的代码(POJO)
Fruits.class

package com.company;

public class Fruits {

  public Fruits() {}
  private int id;
  private String name;

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }
}

FruitsList.class

package com.company;

import java.util.List;

public class FruitsList {

    private List<Fruits> fruitsList;
    public FruitsList(){
    }
    public List<Fruits> getFruitsList() {
        return fruitsList;
    }
    public void setFruitsList(List<Fruits> fruitsList) {
        this.fruitsList = fruitsList;
    }
}
zf9nrax1

zf9nrax11#

预期的输入类型为FruitsList,这是包含数组的对象。您尝试传递的数组为Fruits,这是无效的。
您可以将JSON输入修改为:

{
   "fruitsList":[
      {
         "id":1,
         "name":"one"
      },
      {
         "id":2,
         "name":"second"
      }
   ]
}

相关问题