Apache Camel 的 JsonMappingException
JsonMappingException with Apache Camel
我在使用骆驼路线时遇到异常
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.apache.camel.converter.stream.InputStreamCache and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:284)
at com.fasterxml.jackson.databind.SerializerProvider.mappingException(SerializerProvider.java:1110)
at com.fasterxml.jackson.databind.SerializerProvider.reportMappingProblem(SerializerProvider.java:1135)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:69)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:32)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:292)
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1429)
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1158)
骆驼路线:
restConfiguration().producerComponent("http4").host("localhost").port(9080);
from("direct:getSubscriptions")
.hystrix()
.to("rest:get:testendpoint?carrier={carrier}&flightNumber={flightNumber}&origin={origin}&destination={destination}&date={date}")
.log(">> - ${body}")
.marshal().json(JsonLibrary.Jackson)
.split().jsonpathWriteAsString("$.[1]", true)
.log("${body}");
不确定我做的是否正确?任何建议将不胜感激
在将其编组为 json 之前将您的正文转换为字符串。您可以在元帅之前添加 .convertBodyTo(String.class)
。问题是您正在发送一个流,而杰克逊不知道如何序列化它。
您需要将这样的 jacksonConfig 添加到您的项目中,而不是将 body 转换为字符串:
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
...
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
...
return objectMapper;
}
}
我在使用骆驼路线时遇到异常
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class org.apache.camel.converter.stream.InputStreamCache and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
at com.fasterxml.jackson.databind.JsonMappingException.from(JsonMappingException.java:284)
at com.fasterxml.jackson.databind.SerializerProvider.mappingException(SerializerProvider.java:1110)
at com.fasterxml.jackson.databind.SerializerProvider.reportMappingProblem(SerializerProvider.java:1135)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:69)
at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:32)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:292)
at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1429)
at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1158)
骆驼路线:
restConfiguration().producerComponent("http4").host("localhost").port(9080);
from("direct:getSubscriptions")
.hystrix()
.to("rest:get:testendpoint?carrier={carrier}&flightNumber={flightNumber}&origin={origin}&destination={destination}&date={date}")
.log(">> - ${body}")
.marshal().json(JsonLibrary.Jackson)
.split().jsonpathWriteAsString("$.[1]", true)
.log("${body}");
不确定我做的是否正确?任何建议将不胜感激
在将其编组为 json 之前将您的正文转换为字符串。您可以在元帅之前添加 .convertBodyTo(String.class)
。问题是您正在发送一个流,而杰克逊不知道如何序列化它。
您需要将这样的 jacksonConfig 添加到您的项目中,而不是将 body 转换为字符串:
@Configuration
public class JacksonConfig {
@Bean
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
...
objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
...
return objectMapper;
}
}