Reputation: 8844
I have the following simple class:
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
@JsonIgnoreProperties({ "thirdField" })
public class Message {
private TypeA type;
private String producer;
//Getters and Setters
}
in my test class
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public void testMethd() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.USE_ANNOTATIONS, true);
Class<T> instanceType = Message.class;
String msgBody = "{\"producer\": \"clientApp\", \"type\": \"aType\", \"thirdField\": []}";
objectMapper.readValue(msgBody, instanceType);
}
}
All I am trying to do is to convert the above json string into Message class and ignore the 'thirdField'. But I keep getting
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "thirdField" (class Message), not marked as ignorable (2 known properties: , "type", "producer"])
Upvotes: 26
Views: 60400
Reputation: 41
I found a Solution to this. Try to add
@JsonSerialize(include= JsonSerialize.Inclusion.NON_EMPTY)
About your class
@JsonSerialize(include= JsonSerialize.Inclusion.NON_EMPTY)
class ResponseModel {
//your properties here
@JsonIgnoreProperties("messageList","contactList","sender")
var contactList= ArrayList<ContactModel>()
}
That will solve your issue buddy.
Upvotes: 0
Reputation: 1529
It didn't work for me any of the above answers, i found a workaround that I have reinitialized the object and values (copied the object).
Upvotes: -1
Reputation: 20422
You've mixed different versions of Jackson.
Notice that you import JsonIgnoreProperties
from org.codehaus.jackson.annotate
(version 1.x)
while you're using ObjectMapper
from com.fasterxml.jackson.databind
(version 2.x).
Upvotes: 54
Reputation: 179
Try using the last Jackson version (2.4):
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
@JsonIgnoreProperties({"id"})
Here you can find an example where it's implement using version 2.4: http://www.ibm.com/developerworks/java/library/j-hangman-app/index.html
Upvotes: 6