Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have been upgrading to a later version of jackson (i.e. 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

like image 308
Yanick Salzmann Avatar asked Jan 01 '26 00:01

Yanick Salzmann


1 Answers

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!

like image 197
Yanick Salzmann Avatar answered Jan 02 '26 12:01

Yanick Salzmann