com.fasterxml.jackson.databind.exc.mismatchedinputexception:无法对类型的值进行`com.com.company.fruitslist'从阵列值(令牌' jsontok
我刚刚开始在Java进行编码,所以这个问题听起来很愚蠢。
目标: 我正在尝试构建一个使用Apache Camel路由的Quarkus Rest应用程序,该应用程序具有邮政方法。 The goal is to take an array of fruits as input and add additional information and return it back to the calling application
Input which is being passed to Quarkus Application http://localhost:8080/simple through POSTMAN
[{
"id": 1,
"name": "one"
},{
"id": 2,
"name": "second"
}]
<强>问题: 每当我尝试通过HTTP客户端(Postman)调用其余端点时,我会遇到错误。下面的堆栈跟踪
可以帮助我确定我做错了什么?
{
"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)
水果
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;
}
}
。Classfruitslist.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;
}
}
I just started coding in java, so this question might sound stupid.
Goal:
I am trying to build a Quarkus Rest application which has a POST method using Apache Camel routes. The goal is to take an array of fruits as input and add additional information and return it back to the calling application
Input which is being passed to Quarkus Application http://localhost:8080/simple through POSTMAN
[{
"id": 1,
"name": "one"
},{
"id": 2,
"name": "second"
}]
Issue:
Whenever i try to call the REST endpoint through a HTTP client (POSTMAN), I am getting an error. Stack trace below
Can someone please help me identify what am i doing wrong ?
{
"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)"
Here is my Route code:
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);
}
});
}
}
Below are the FruitsList, Fruits and AddressRequest Class code (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;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
预期的输入类型为
fruitslist
,它是包含数组的对象。您正在尝试传递fruits
的数组,这是无效的。您可以将JSON输入修改为:
The expected input type is
FruitsList
, which is an object that contains an array. You're attempting to pass an array ofFruits
, which is not valid.You can modify the JSON input as: