Ingo D Ingo D - 8 days ago 5
reST (reStructuredText) Question

Unmarshaling JSON with Apache Camel

I use the Apache Camel component http4 to request data from a REST web service.

The response type of this REST service is JSON.

{"coord":{"lon":145.77,"lat":-16.92},"weather":
[{"id":802,"main":"Clouds","description":"scattered
clouds","icon":"03n"}],"base":"stations",
....


I have a JSON annotated Java Class which reflects the JSON structure

@JsonPropertyOrder({
"coord",
"weather",
"base",
...

})
public class Report {

@JsonProperty("coord")
private Coord coord;
@JsonProperty("weather")
private List<Weather> weather = new ArrayList<Weather>();
@JsonProperty("base")
private String base;

...


Within the Camel Route I defined a JsonDataFormat which is referring to the annotated class.

...
public void configure() throws Exception {
JsonDataFormat jsonDataFormat = new JsonDataFormat();
jsonDataFormat.setUnmarshalType(Report.class);

from(timer....).to("http4://${url}").unmarshal(jsonDataFormat).bean(myService);


this results in an Exception:


com.thoughtworks.xstream.mapper.CannotResolveClassException: coord at
com.thoughtworks.xstream.mapper.DefaultMapper.realClass(DefaultMapper.java:81)
~[xstream-1.4.9.jar:1.4.9]



  • I tried to use the class Coord as unmarshalType

  • I used the fully qualified name for the unmarshalType

  • I also tried a pure Pojo without JSON-Annotations


Answer

Can you try specifying Jackson as the JsonLibrary of your data format? The annotations you use are Jackson's yet XStream tries to perform the unmarshal:

JsonDataFormat jsonDataFormat = new JsonDataFormat(JsonLibrary.Jackson);
jsonDataFormat.setUnmarshalType(Report.class);

From everyhting I've seen this should work.