Spring 5.0.4 & Jackson2 在启动时抛出异常
Spring 5.0.4 & Jackson2 throws excepiton on startup
我正在尝试制作简单的 REST 服务,但遇到了 2 个问题。
1.REST控制器的任何方法returns 406 http错误;
@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();
}
所以 google 告诉我,问题可能在于缺少 Jackson2 依赖项。
但是添加此依赖项会导致此错误,添加较低版本会导致相同的输出或 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
我做错了什么?
在Spring5或以上,BeanInstantiationExceptionclass负责抛出java.lang.NoClassDefFoundError:com/fasterxml/jackson/databind/exc/InvalidDefinitionException 作为嵌套异常。
从上述异常可以看出,缺少此异常的根本原因 class InvalidDefinitionException。这个 class 自 2.9.0 版本起在 jackson databind API 中引入。
因此,要解决此问题,只需将 jackson 数据绑定依赖项添加到 2.9.0 或更高版本
<依赖关系>
com.fasterxml.jackson.core
jackson-databind
<版本>2.9.0版本>
谢谢!!
我正在尝试制作简单的 REST 服务,但遇到了 2 个问题。 1.REST控制器的任何方法returns 406 http错误;
@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();
}
所以 google 告诉我,问题可能在于缺少 Jackson2 依赖项。 但是添加此依赖项会导致此错误,添加较低版本会导致相同的输出或 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
我做错了什么?
在Spring5或以上,BeanInstantiationExceptionclass负责抛出java.lang.NoClassDefFoundError:com/fasterxml/jackson/databind/exc/InvalidDefinitionException 作为嵌套异常。
从上述异常可以看出,缺少此异常的根本原因 class InvalidDefinitionException。这个 class 自 2.9.0 版本起在 jackson databind API 中引入。
因此,要解决此问题,只需将 jackson 数据绑定依赖项添加到 2.9.0 或更高版本
<依赖关系>
<版本>2.9.0版本>
谢谢!!