26

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"])
kk1957
  • 8,246
  • 10
  • 41
  • 63

4 Answers4

51

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).

Lukasz Wiktor
  • 19,644
  • 5
  • 69
  • 82
  • 1
    Good catch. I changed it to import com.fasterxml.jackson.annotation.JsonIgnoreProperties; but still get the same exception – kk1957 Jan 08 '14 at 15:13
  • 1
    @kk1957 I reproduced your case with the only difference that I imported JsonIgnoreProperties from com.fasterxml.jackson.annotation and it worked - no exceptions, message object was deserialized corectly. Try to checkout https://github.com/LukaszWiktor/json-ignore-properties-test and run Test.main() – Lukasz Wiktor Jan 09 '14 at 12:04
  • Yeah, looks like there were other things wrong in my set up. Thanks for the help. – kk1957 Jan 09 '14 at 15:47
  • 1
    Thank you, I was looking what the hell I was doing wrong following examples at https://github.com/helun/Ektorp while using latest version of jackson lib. – Miha Pirnat Feb 28 '17 at 10:00
  • 1
    @MihaPirnat I'm glad that my answer is still helpful after 3 years. – Lukasz Wiktor Feb 28 '17 at 10:27
  • @LukaszWiktor yeah, their tutorial still has old example and novices like me still fall for it: Here's an trivial example class: import org.codehaus.jackson.annotate.*; – Miha Pirnat Feb 28 '17 at 10:35
  • flawless answer! Saved the day! – Gaurav May 21 '20 at 12:04
  • 1
    @gaurav It's amazing that my answer is still helpful after over 6 years! – Lukasz Wiktor May 21 '20 at 17:19
5

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

li_developer
  • 139
  • 1
  • 2
0

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.

-1

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).

Senthil Arumugam SP
  • 1,461
  • 12
  • 17