我刚开始用java编程,所以这个问题可能听起来很蠢。
目标:我正在尝试构建一个Quarkus Rest应用程序,它有一个POST方法使用Apache Camel路由。目标是将水果数组作为输入,添加附加信息并将其返回给调用应用程序
通过POSTMAN
传递给Quarkus Application 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]rntat com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:59)rntat com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1741)rntat com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1515)rntat com.fasterxml.jackson.databind.DeserializationContext.handleUnexpectedToken(DeserializationContext.java:1462)rntat com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeFromArray(BeanDeserializer.java:638)rntat com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:210)rntat com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:186)rntat com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:322)rntat com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2051)rntat com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1427)rntat com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1442)rntat org.apache.camel.component.jackson.AbstractJacksonDataFormat.unmarshal(AbstractJacksonDataFormat.java:188)rntat org.apache.camel.support.processor.UnmarshalProcessor.process(UnmarshalProcessor.java:64)rntat org.apache.camel.impl.engine.DefaultAsyncProcessorAwaitManager.process(DefaultAsyncProcessorAwaitManager.java:83)rntat org.apache.camel.support.AsyncProcessorSupport.process(AsyncProcessorSupport.java:41)rntat org.apache.camel.processor.RestBindingAdvice.unmarshal(RestBindingAdvice.java:307)rntat org.apache.camel.processor.RestBindingAdvice.before(RestBindingAdvice.java:142)rntat org.apache.camel.processor.RestBindingAdvice.before(RestBindingAdvice.java:51)rntat org.apache.camel.impl.engine.CamelInternalProcessor.process(CamelInternalProcessor.java:341)rntat org.apache.camel.component.platform.http.vertx.VertxPlatformHttpConsumer.lambda$handleRequest$2(VertxPlatformHttpConsumer.java:185)rntat io.vertx.core.impl.ContextImpl.lambda$null$0(ContextImpl.java:159)rntat io.vertx.core.impl.AbstractContext.dispatch(AbstractContext.java:100)rntat io.vertx.core.impl.ContextImpl.lambda$executeBlocking$1(ContextImpl.java:157)rntat org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)rntat org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)rntat org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)rntat org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)rntat org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)rntat io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)rntat 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;
}
}
期望的输入类型是FruitsList
,它是一个包含数组的对象。您正在尝试传递一个Fruits
数组,这是无效的。
您可以将JSON输入修改为:
{
"fruitsList":[
{
"id":1,
"name":"one"
},
{
"id":2,
"name":"second"
}
]
}