How to force `.andExpect(jsonPath()` to return Long/long instead int for int number for jackson parser

Update

  • Spring Framework 4.3.3 and 5.0.0 added first-class support for explicit conversions for request content for use with MockRestServiceServer.
    • See SPR-14498 for details.
  • Spring Framework 4.3.15 and 5.0.5 will add first-class support for explicit conversions for response content for use with MockMvc.
    • See SPR-16587 for details.

Original Answer

One option (which I haven not personally verified) would be to try a different JsonProvider. This can be set via com.jayway.jsonpath.Configuration.setDefaults(Defaults).

If you are sure that the Long can always be safely narrowed to an int, you could use the following:

andExpect(jsonPath("$[1].parent_id",is(this.transactions.get(1).getParentId().intValue())));

And the only other option is to write a custom Matcher that converts the incoming Integer to a Long before performing the actual matching.


Since Spring 5.2 you can provide type as a third argument of jsonPath() method:

public static <T> ResultMatcher jsonPath(String expression, Matcher<T> matcher, Class<T> targetType)

So in this case it will be:

andExpect(jsonPath("$[1].parent_id",is(this.transactions.get(1).getParentId()), Long.class));

Source: Spring Javadoc API