Jackson unrecognized field exception but field is in JSON
This works fine for me...
@Test
public void test() throws Exception {
MyMessages mm = new MyMessages();
MyMessage m = new MyMessage();
m.setMsgId(1);
m.setText("foo");
mm.setMessages(Arrays.asList(m));
mm.setTimestamp("123");
@SuppressWarnings("deprecation")
ObjectToJsonTransformer otjt = new ObjectToJsonTransformer(new ObjectMapper());
Message<?> message = new GenericMessage<MyMessages>(mm);
message = otjt.transform(message);
System.out.println(message);
@SuppressWarnings("deprecation")
JsonToObjectTransformer<MyMessages> jtot = new JsonToObjectTransformer<MyMessages>(MyMessages.class, new ObjectMapper());
message = jtot.transform(message);
mm = (MyMessages) message.getPayload();
System.out.println(mm.getTimestamp());
System.out.println(mm.getMessages().get(0).getText());
}
(I changed your classnames slightly to avoid colliding with Message<?>
)
Resulting in...
[Payload={"timestamp":"123","msgs":[{"msgId":1,"msgText":"foo"}]}][Headers={timestamp=1373997151738, id=f2425f36-a500-4aee-93a4-e7e0240ce0f1, content-type=application/json}]
123
foo
Do you have both jackson 1.x (codehaus) and 2.x (fasterxml) on the classpath, and using Spring Integration 3.0.0?
If they're both on the classpath, SI will use Jackson 2.x, by default, (which won't understand 1.x annotations).
Or, I guess - are you using Jackson2 (fasterxml) annotations? Spring Integration 2.x uses Jackson 1 (codehaus).
EDIT:
In order to support both versions of Jackson, you can annotate the class with both annotations...
@JsonProperty("msgs")
@com.fasterxml.jackson.annotation.JsonProperty("msgs")
public List<MyMessage> messages;
Comments
-
C0deAttack almost 2 years
I am using Spring Integration to consume a message with a JSON Payload.
In my spring context I have
<integration:channel id="jsonToMyMessageConverterChannel"/> <integration:json-to-object-transformer type="com.acme.messaging.message.MyMessage" input-channel="jsonToMyMessageConverterChannel" output-channel="myMessageUpdateChannel"/>
My message related objects are:
MyMessage.java
@JsonIgnoreProperties(ignoreUnknown=true) public class MyMessage { @JsonProperty private String timestamp; @JsonProperty("msgs") private List<Message> messages; // Getters and Setters... }
Message.java
@JsonIgnoreProperties(ignoreUnknown=true) public class Message { @JsonProperty private Integer msgId; @JsonProperty("msgText") private String text; // Getters and Setters... }
When the json transformer attempts to convert the message to an object it fails with
Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "msgs" (Class com.acme.messaging.message.MyMessage), not marked as ignorable
The JSON payload definitely has
msgs
which is an array that has objects which represent theMessage.java
class.Can any one suggest reasons why the exception occurs given that the JSON has the field that is being complained about and the class itself is also annotated to ignore unknown fields?
Update After some debugging it looks like the
@JsonProperty("msgs")
annotations aren't being use, for some reason. -
C0deAttack almost 11 yearsI am using Spring Integration 2.2.3, Fasterxml Jackson (core|databind|annotations) 2.2.1 and Codehaus Jackson (mapper-asl|core-asl) 1.7.4. The @JsonProperty annotation is imported from jackson-annotations-2.2.1. So your last comment might be the reason, I will investigate.
-
C0deAttack almost 11 yearsYou comment at the bottom led me to the solution. I didn't know that Spring Integration 2.x used Codehaus Jackson (1.9.x). I had imported the Fastxml Jackson classes instead. Thanks!
-
Gary Russell almost 11 yearsI edited the answer indicating you can add both sets of annotations.