简体   繁体   中英

jackson-databind "object is not an instance of declaring class"

I have been upgrading to a later version of jackson (ie from org.codehaus... to com.fasterxml...) and suddenly I am facing many weird errors. After hours of trying and adjusting I still cant get it to work so I am asking you guys if you can help me.

I have the following method:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("getTerminalsByIdAndLocation")
List<SearchResultDto> getTerminalsByIdAndLocation(@QueryParam("location") String location, @QueryParam("id") Integer id) throws BusinessException;

and that functions implementation just does a lookup in a repository.

The SearchResultDto looks like this:

@JsonIgnoreProperties(ignoreUnknown = true)
public class SearchResultDto implements Serializable {
    private static final long serialVersionUID = 1L;

    private TerminalId terminalId;
    private Integer location;
    private String streetNumber;
    private String postalcoldeCity;
    private Status status;

   // getters and setters with no annotation or so
}

When I am now calling my method I am getting the following error:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: object is not an instance of declaring class (through reference chain: java.util.ArrayList[0]-><long package>.SearchResultDto["terminalId"])

After a lot of trying I thought I will just remove the terminalId and then it changes to:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: object is not an instance of declaring class (through reference chain: java.util.ArrayList[0]-><long package>AtmSearchResultDto["location"])

I am clueless, what is wrong here?

EDIT

I also tried using @JsonIgnore on everything except String streetNumber but then the same exception happens just for streetNumber

Long story short: I messed up my class path and there were two class loaders, the implementation of the REST method called a repository from the database module from where it got the instance from a different class loader. After adjusting my maven scopes and import types it is now working!

I had a similar issue for me the problem was that one of my POJOs was final . Removing the final keyword did the trick.

public final class AccessControlMap extends HashMap<Permission, Set<AccessType>>

//Before
public final class AccessType {

//After
public class AccessType {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM