I have a rest api that returns a String:
@GetMapping("/api/users")
public String getUsers(){
return "DENIS";
}
I'm calling this api from apache camel:
from("direct:start")
.setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to("http://localhost:8085/api/users")
.unmarshal().json(JsonLibrary.Jackson, String.class);
val template = DefaultFluentProducerTemplate.on(camelContext);
String a = template.to("direct://" + "start").request(String.class);
These actions result in this error:
com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'DENIS': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (org.apache.camel.converter.stream.CachedOutputStream$WrappedInputStream); line: 1, column: 6]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:2337) ~[jackson-core-2.12.1.jar:2.12.1]
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:720) ~[jackson-core-2.12.1.jar:2.12.1]
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._reportInvalidToken(UTF8StreamJsonParser.java:3593) ~[jackson-core-2.12.1.jar:2.12.1]
Moreover, here's another strange behavior:
@Override
public void configure() {
from("timer://test?period=2000")
.setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to("http://localhost:8085/api/users")
.process(new Processor() {
@Override
public void process(Exchange exchange) {
String body = exchange.getIn().getBody(String.class);
}
});
}
}
If the code is like this, then the string is deserialized, but if the string location is some object, it will always be null, there will be no error, it will be null. Although in the debugger it will be visible that the object has arrived, its fields, but camel does not see it.
I have already tried many options, but I can not understand what is the matter. If I call the api that returns boolean, and I accept it, then everything is ok, but it doesn't work with objects and strings.
How can I fix the situation? What could be the reason? I've already tried playing with dependencies, but there were no results.
I would appreciate your help
The first case decided so:
from("direct:start")
.setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to("http://localhost:8085/api/users")
.unmarshal(new JacksonDataFormat(User[].class));
with the help JacksonDataFormat.
The second option could not be completely solved, here's what happened:
from("direct:start")
.setHeader(Exchange.HTTP_METHOD).constant(HttpMethod.GET)
.setHeader(Exchange.CONTENT_TYPE, constant("application/json"))
.to("http://localhost:8085/api/users")
.to("direct:httpClient")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws JsonProcessingException {
String body = exchange.getIn().getBody(String.class);
User[] users = jacksonDataFormat.getObjectMapper().readValue(body, User[].class);
exchange.getIn().setBody(users);
}
})
.to("direct:httpClient");
The body must be turned first into a String, and then String into a User. If you try to turn the body immediately into a User, it will always be null, I can't understand why.
The question is partially resolved, I will ask the second part again on this site (Apache Camel: I can't get an object out of the body and transform it)