Spring-boot 项目中使用 jackson 遇到的一个问题
jackson介绍
java代码中实现序列化和反序列化的工具类
jackson使用Demo
https://github.com/Naylor55/JavaDebrisCode/tree/branch_Java-Serializable/javaserializable
Ideal控制台报错信息
2019-05-22 15:43:20.333 [main] ERROR org.springframework.boot.SpringApplication.reportFailure:837 - Application run failed java.lang.AbstractMethodError: com.fasterxml.jackson.core.type.ResolvedType.getReferencedType()Lcom/fasterxml/jackson/core/type/ResolvedType; at com.fasterxml.jackson.core.type.ResolvedType.isReferenceType(ResolvedType.java:59) at com.fasterxml.jackson.datatype.jdk8.Jdk8TypeModifier.modifyType(Jdk8TypeModifier.java:20) at com.fasterxml.jackson.databind.type.TypeFactory._constructType(TypeFactory.java:413) at com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:354) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.getJavaType(AbstractJackson2HttpMessageConverter.java:323) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.canRead(AbstractJackson2HttpMessageConverter.java:158) at org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter.canRead(AbstractJackson2HttpMessageConverter.java:150) at org.springframework.web.client.RestTemplate$AcceptHeaderRequestCallback.doWithRequest(RestTemplate.java:805) Disconnected from the target VM, address: '127.0.0.1:57769', transport: 'socket' at org.springframework.web.client.RestTemplate$HttpEntityRequestCallback.doWithRequest(RestTemplate.java:868) at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:685) at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:644) at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:564) at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.getRemoteEnvironment(ConfigServicePropertySourceLocator.java:218) at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:96) at org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration.initialize(PropertySourceBootstrapConfiguration.java:94) at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:628) at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:364) at org.springframework.boot.SpringApplication.run(SpringApplication.java:305) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1242) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1230) at com.leading.WarehouseserviceApplication.main(WarehouseserviceApplication.java:50)
报错原因
Spring-Boot框架自身默认就引用了jackson,如果你在自己的开发项目中再次引用,就会有冲突。然而,JVM 不会告诉你是引用有冲突,而是直接给你一个类型有问题的提示。意思就是我只能告诉你类型有问题,具体的你自己查吧。IDEAL 作为一个开发工具,在这点上面也没有做出点贡献。Java这个生态缺陷还是很大的!!!
spriing-boot 默认引用
自己项目的pom中找到:org.springframework.boot , 并点击进去。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath /> </parent>
然后再找:spriing-boot-dependencies ,并点击进去
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.0.6.RELEASE</version> <relativePath>../../spring-boot-dependencies</relativePath> </parent>
然后就可以看到spring-boot 对jackson 的引用,如下图
解决办法
在当前开发项目的pom文件中删除对jackson的依赖,即删除如下图所示