JsonMappingException with Apache Camel
Asked Answered
S

3

5

I am getting below exception with Camel Route

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)

Camel Route:

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}");

Not sure I am doing it correct? Any suggestions will be appreciated

Sevenfold answered 15/2, 2018 at 1:40 Comment(0)
S
8

Convert your body to string before marshaling it to json. You can add .convertBodyTo(String.class) before your marshal. The issue is that you are sending a stream and jackson doesn't know how to serialize it.

Spidery answered 15/2, 2018 at 6:23 Comment(2)
Thanks. This worked. I have spent around half a day searching around this thing and it is not really clear from Camel website nor it is mentioned in Camel in Action Second Edition. I think Camel community should really focus on getting their documentation correct or at least usable so that learning curve does not become so stiff.Sevenfold
Well, the Camel documentation is getting a major update. So stay tuned for that and the idea to convert to string is mentioned in many posts. But if you look at the stacktrace you get hint that about this because it says "No serializer found..."Spidery
K
1

If there is someone with this problem and this code

.to(endpoint)
            .log("${body}")
            .unmarshal( new JacksonDataFormat(yourClassModel.class))

the only thing you have to do is delete the .log("${body}") because the log steal your response so the unmarshal can´t find content, this is how to function

    .to(endpoint)
            .unmarshal( new JacksonDataFormat(yourClassModel.class))
Kidnap answered 12/12, 2022 at 16:28 Comment(0)
P
0

You need to add jacksonConfig like this to your project instead of converting body to String:

@Configuration
public class JacksonConfig {

    @Bean
    public ObjectMapper objectMapper() {

        ObjectMapper objectMapper = new ObjectMapper();
        ...
        objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        ...

        return objectMapper;
    }
}
Predacious answered 10/9, 2021 at 14:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.