亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

使用SpringBoot項(xiàng)目導(dǎo)入openfeign版本的問(wèn)題

 更新時(shí)間:2022年03月01日 09:40:23   作者:懶懶的小伙子  
這篇文章主要介紹了使用SpringBoot項(xiàng)目導(dǎo)入openfeign版本的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringBoot項(xiàng)目導(dǎo)入openfeign版本

記SpringBoot項(xiàng)目導(dǎo)入spring-cloud-starter-openfeign依賴(lài)版本問(wèn)題

前幾天在寫(xiě)項(xiàng)目時(shí),需要微服務(wù)間的調(diào)用,于是決定使用openfeign。

由于我們的項(xiàng)目使用的是Spring-Boot,版本是2.1.6。

在初次導(dǎo)入openfeign時(shí)使用的版本時(shí)2.2.2,但是將依賴(lài)導(dǎo)入后,發(fā)現(xiàn)原先的項(xiàng)目啟動(dòng)報(bào)錯(cuò)

 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bootstrapImportSelectorConfiguration': Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationPropertiesBean
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:845) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:742) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:389) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:311) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:139) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.cloud.bootstrap.BootstrapApplicationListener.bootstrapServiceContext(BootstrapApplicationListener.java:212) ~[spring-cloud-context-2.2.2.RELEASE.jar:2.2.2.RELEASE]
 at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:117) ~[spring-cloud-context-2.2.2.RELEASE.jar:2.2.2.RELEASE]
 at org.springframework.cloud.bootstrap.BootstrapApplicationListener.onApplicationEvent(BootstrapApplicationListener.java:74) ~[spring-cloud-context-2.2.2.RELEASE.jar:2.2.2.RELEASE]
 at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:75) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:53) ~[spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:340) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:304) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1213) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1202) [spring-boot-2.1.6.RELEASE.jar:2.1.6.RELEASE]
 at com.wjs.plan.PlanApplication.main(PlanApplication.java:16) [classes/:na]
Caused by: java.lang.NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationPropertiesBean
 at org.springframework.cloud.context.properties.ConfigurationPropertiesBeans.postProcessBeforeInitialization(ConfigurationPropertiesBeans.java:94) ~[spring-cloud-context-2.2.2.RELEASE.jar:2.2.2.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:414) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1770) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593) ~[spring-beans-5.1.8.RELEASE.jar:5.1.8.RELEASE]
 ... 26 common frames omitted
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.context.properties.ConfigurationPropertiesBean
 at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_201]
 at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_201]
 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) ~[na:1.8.0_201]
 at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_201]
 ... 30 common frames omitted

找了一大推資料最后將openfeign版本改成2.1.1發(fā)現(xiàn)項(xiàng)目可以正常啟動(dòng)了。

最后查看了openfengn兩個(gè)版本的不同,再次貼出來(lái)。

版本2.1.1

版本2.2.2

相信在這里大佬已經(jīng)看出來(lái)了,openfeign2.2.2版本中指定了每個(gè)jar包的版本,2.1.1版本則是引入了父工程的版本號(hào)。

在引入openfeign2.2.2后可以看到spring-cloud-context有2個(gè),這就導(dǎo)致jar包沖突從而項(xiàng)目啟動(dòng)失敗。

被SpringBoot整合OpenFeign坑慘了

最近,在使用SpringBoot+K8S開(kāi)發(fā)微服務(wù)系統(tǒng),既然使用了K8S,我就不想使用SpringCloud了。

為啥,因?yàn)镵8S本身的就提供了非常6的服務(wù)注冊(cè)與發(fā)現(xiàn)、限流、熔斷、負(fù)載均衡等等微服務(wù)需要使用的技術(shù),那我為啥還要接入SpringCloud呢?額,說(shuō)了這么多,在真正使用SpringBoot+K8S這一套技術(shù)棧的時(shí)候,也會(huì)遇到一些問(wèn)題,比如我不需要使用SpringCloud時(shí),調(diào)用其他服務(wù)時(shí),我使用的是原生的OpenFegin,在使用OpenFegin調(diào)用其他服務(wù)的時(shí)候,就遇到了一個(gè)大坑。

通過(guò)OpenFeign請(qǐng)求返回值LocalDateTime發(fā)生了異常,今天,我們就來(lái)說(shuō)說(shuō)這個(gè)坑!

好了,我們開(kāi)始吧~~

項(xiàng)目集成OpenFegin

集成OpenFegin依賴(lài)

首先,我先跟大家說(shuō)下項(xiàng)目的配置,整體項(xiàng)目使用的SpringBoot版本為2.2.6,原生的OpenFegin使用的是11.0,我們通過(guò)如下方式在pom.xml中引入OpenFegin。

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <skip_maven_deploy>false</skip_maven_deploy>
    <java.version>1.8</java.version>
    <openfegin.version>11.0</openfegin.version>
</properties>
<dependencies>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-core</artifactId>
        <version>${openfegin.version}</version>
    </dependency>
    <dependency>
        <groupId>io.github.openfeign</groupId>
        <artifactId>feign-jackson</artifactId>
        <version>${openfegin.version}</version>
    </dependency>
</dependencies>

這里,我省略了一些其他的配置項(xiàng)。

接下來(lái),我就開(kāi)始在我的項(xiàng)目中使用OpenFegin調(diào)用遠(yuǎn)程服務(wù)了。具體步驟如下。

實(shí)現(xiàn)遠(yuǎn)程調(diào)用

首先,創(chuàng)建OpenFeignConfig類(lèi),配置OpenFegin默認(rèn)使用的Contract。

@Configuration
public class OpenFeignConfig {
?? ?@Bean
?? ?public Contract useFeignAnnotations() {
?? ??? ?return new Contract.Default();
?? ?}
}

接下來(lái),我們寫(xiě)一個(gè)通用的獲取OpenFeign客戶(hù)端的工廠(chǎng)類(lèi),這個(gè)類(lèi)也比較簡(jiǎn)單,本質(zhì)上就是以一個(gè)HashMap來(lái)緩存所有的FeginClient,這個(gè)的FeginClient本質(zhì)上就是我們自定義的Fegin接口,緩存中的Key為請(qǐng)求連接的基礎(chǔ)URL,緩存的Value就是我們定義的FeginClient接口。

public class FeginClientFactory {?
?? ?/**
?? ? * 緩存所有的Fegin客戶(hù)端
?? ? */
?? ?private volatile static Map<String, Object> feginClientCache = new HashMap<>();
?? ?
?? ?/**
?? ? * 從Map中獲取數(shù)據(jù)
?? ? * @return?
?? ? */
?? ?@SuppressWarnings("unchecked")
?? ?public static <T> T getFeginClient(Class<T> clazz, String baseUrl){
?? ??? ?if(!feginClientCache.containsKey(baseUrl)) {
?? ??? ??? ?synchronized (FeginClientFactory.class) {
?? ??? ??? ??? ?if(!feginClientCache.containsKey(baseUrl)) {
?? ??? ??? ??? ??? ?T feginClient = Feign.builder().decoder(new JacksonDecoder()).encoder(new JacksonEncoder()).target(clazz, baseUrl);
?? ??? ??? ??? ??? ?feginClientCache.put(baseUrl, feginClient);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ??? ?return (T)feginClientCache.get(baseUrl);
?? ?}
}

接下來(lái),我們就定義一個(gè)FeginClient接口。

public interface FeginClientProxy {
?? ?@Headers("Content-Type:application/json;charset=UTF-8")
?? ?@RequestLine("POST /user/login")
?? ?UserLoginVo login(UserLoginVo loginVo);
}

接下來(lái),我們創(chuàng)建SpringBoot的測(cè)試類(lèi)。

@RunWith(SpringRunner.class)
@SpringBootTest
public class IcpsWeightStarterTest {
?? ?@Test
?? ?public void testUserLogin() {
?? ??? ?ResponseMessage result = FeginClientFactory.getFeginClient(FeginClientProxy.class, "http://127.0.0.1").login(new UserLoginVo("zhangsan", "123456", 1));
?? ??? ?System.out.println(JsonUtils.bean2Json(result));
?? ?}
}

一切準(zhǔn)備就緒,運(yùn)行測(cè)試。麻蛋,出問(wèn)題了。主要的問(wèn)題就是通過(guò)OpenFeign請(qǐng)求返回值LocalDateTime字段會(huì)發(fā)生異常?。?!

注:此時(shí)異常時(shí),我們?cè)贚ocalDateTime字段上添加的注解如下所示。

import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
@TableField(value = "CREATE_TIME", fill = FieldFill.INSERT)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
private LocalDateTime createTime;

解決問(wèn)題

問(wèn)題描述

SpringBoot通過(guò)原生OpenFeign客戶(hù)端調(diào)用HTTP接口,如果返回值中包含LocalDateTime類(lèi)型(包括其他JSR-310中java.time包的時(shí)間類(lèi)),在客戶(hù)端可能會(huì)出現(xiàn)反序列化失敗的錯(cuò)誤。錯(cuò)誤信息如下:

 Caused by:com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-10-07T11:04:32')

問(wèn)題分析

從客戶(hù)端調(diào)用fegin,也是相當(dāng)于URL傳參就相當(dāng)于經(jīng)過(guò)一次JSON轉(zhuǎn)換,數(shù)據(jù)庫(kù)取出‘2020-10-07T11:04:32’數(shù)據(jù)這時(shí)是時(shí)間類(lèi)型,進(jìn)過(guò)JSON之后就變成了String類(lèi)型,T就變成了字符不再是一個(gè)特殊字符,因此String的字符串“2020-10-07T11:04:32”反序列化就會(huì)失敗。

問(wèn)題解決

在項(xiàng)目中增加依賴(lài)。

<dependency>
? ? <groupId>com.fasterxml.jackson.datatype</groupId>
? ? <artifactId>jackson-datatype-jsr310</artifactId>
? ? <version>2.9.9</version>
</dependency>

注:如果是用的是SpringBoot,并且明確指定了SpringBoot版本,引入jackson-datatype-jsr310時(shí),可以不用指定版本號(hào)。

接下來(lái),在POJO類(lèi)的LocalDateTime類(lèi)型字段增加如下注解。

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;

添加后的效果如下所示。

import java.time.LocalDateTime;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
@TableField(value = "CREATE_TIME", fill = FieldFill.INSERT)
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
private LocalDateTime createTime;

此時(shí),再次調(diào)用遠(yuǎn)程接口,問(wèn)題解決。

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringBoot加載應(yīng)用事件監(jiān)聽(tīng)器代碼實(shí)例

    SpringBoot加載應(yīng)用事件監(jiān)聽(tīng)器代碼實(shí)例

    這篇文章主要介紹了SpringBoot加載應(yīng)用事件監(jiān)聽(tīng)器代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • SpringBoot 如何實(shí)時(shí)刷新靜態(tài)文件

    SpringBoot 如何實(shí)時(shí)刷新靜態(tài)文件

    這篇文章主要介紹了SpringBoot如何實(shí)時(shí)刷新靜態(tài)文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • java中Callback簡(jiǎn)單使用總結(jié)

    java中Callback簡(jiǎn)單使用總結(jié)

    正好學(xué)習(xí)到j(luò)ava Callback,就整理了一下,希望整理的文章內(nèi)容對(duì)大家有所幫助
    2017-04-04
  • MyBatis-Plus Generator配置詳解

    MyBatis-Plus Generator配置詳解

    這篇文章主要介紹了MyBatis-Plus Generator配置詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java中的?HTTP?協(xié)議原理詳解

    Java中的?HTTP?協(xié)議原理詳解

    這篇文章主要介紹了Java中的?HTTP?協(xié)議原理詳解,HTTP超文本傳輸協(xié)議,下文簡(jiǎn)稱(chēng)?HTTP,它的作用是用于實(shí)現(xiàn)服務(wù)器端和客戶(hù)端的數(shù)據(jù)傳輸?shù)?/div> 2022-07-07
  • Java無(wú)界阻塞隊(duì)列DelayQueue詳細(xì)解析

    Java無(wú)界阻塞隊(duì)列DelayQueue詳細(xì)解析

    這篇文章主要介紹了Java無(wú)界阻塞隊(duì)列DelayQueue詳細(xì)解析,DelayQueue是一個(gè)支持時(shí)延獲取元素的無(wú)界阻塞隊(duì)列,隊(duì)列使用PriorityQueue來(lái)實(shí)現(xiàn),隊(duì)列中的元素必須實(shí)現(xiàn)Delayed接口,在創(chuàng)建元素時(shí)可以指定多久才能從隊(duì)列中獲取當(dāng)前元素,需要的朋友可以參考下
    2023-12-12
  • Java8特性使用Function代替分支語(yǔ)句

    Java8特性使用Function代替分支語(yǔ)句

    這篇文章主要介紹了Java8特性使用Function代替分支語(yǔ)句,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-09-09
  • logback之如何按日期和大小切分日志

    logback之如何按日期和大小切分日志

    這篇文章主要介紹了logback之如何按日期和大小切分日志問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 一文簡(jiǎn)介Java中BlockingQueue阻塞隊(duì)列

    一文簡(jiǎn)介Java中BlockingQueue阻塞隊(duì)列

    本文主要介紹了一文簡(jiǎn)介Java中BlockingQueue阻塞隊(duì)列,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-06-06
  • jdk1.8中的for循環(huán)問(wèn)題記錄

    jdk1.8中的for循環(huán)問(wèn)題記錄

    這篇文章主要介紹了jdk1.8中的for循環(huán)及jdk1.8 新特性之 forEach 循環(huán)遍歷問(wèn)題,本文通過(guò)實(shí)例代碼給大家詳細(xì)講解,需要的朋友可以參考下
    2022-11-11

最新評(píng)論