Search code examples
javaspringrestjackson2

Spring 5.0.4 & Jackson2 throws excepiton on startup


I am trying to make simple REST service but faced with 2 problems. 1. Any method of REST controller returns 406 http error;

@GetMapping(value = "/getUser/{id}", headers = "Accept=application/json",
        produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public ResponseEntity<Object> getUser(@PathVariable(value = "id")Long id) {
    if (id == null) return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    UserInfo userInfo = userService.get(id);


    if (userInfo == null) return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    return new ResponseEntity<>(userInfo, HttpStatus.OK);
}
@GetMapping(value = "/getUsers", headers = "Accept=application/json",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public List<UserInfo> getUsers()
{
    return userService.getAll();
}

So google told me, that problem can be in missing Jackson2 dependencies. But adding this dependencies cause this error, adding lower versions either results the same output or 406

 <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.4</version>
    </dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.4.4</version>
</dependency>


> 06-May-2018 15:36:45.477 WARNING [http-nio-25565-exec-3]
> org.springframework.context.support.AbstractApplicationContext.refresh
> Exception encountered during context initialization - cancelling
> refresh attempt:
> org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'requestMappingHandlerAdapter' defined in
> class path resource
> [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]:
> Bean instantiation via factory method failed; nested exception is
> org.springframework.beans.BeanInstantiationException: Failed to
> instantiate
> [org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter]:
> Factory method 'requestMappingHandlerAdapter' threw exception; nested
> exception is java.lang.NoClassDefFoundError:
> com/fasterxml/jackson/databind/exc/InvalidDefinitionException

What am I doing wrong?


Solution

  • In Spring 5 or above, BeanInstantiationException class is responsible for throwing java.lang.NoClassDefFoundError: com/fasterxml/jackson/databind/exc/InvalidDefinitionException as a nested exception.

    As evident from the above exception the root cause of this exception is missing class InvalidDefinitionException. This class has been introduced in jackson databind API since 2.9.0 version onwards.

    So to resolve this issue simply add jackson databind dependency with version 2.9.0 or above

    <dependency>
           <groupId>com.fasterxml.jackson.core</groupId>
           <artifactId>jackson-databind</artifactId>
           <version>2.9.0</version>
    </dependency>

    Thanks !!