最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

@FeignClient?path屬性路徑前綴帶路徑變量時(shí)報(bào)錯(cuò)的解決

 更新時(shí)間:2022年07月04日 09:44:20   作者:路過(guò)君_P  
這篇文章主要介紹了@FeignClient?path屬性路徑前綴帶路徑變量時(shí)報(bào)錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

@FeignClient path屬性路徑前綴帶路徑變量時(shí)報(bào)錯(cuò)

現(xiàn)象

FeignClient注解中使用path屬性定義url前綴時(shí),如果使用了路徑變量,則會(huì)報(bào)錯(cuò)

例如

@FeignClient(name = "user-api",
path = "/api/user/{id}")

報(bào)錯(cuò)

ERROR o.a.c.c.C.[.[localhost].[/].[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: Target is not a valid URI.] with root cause
java.net.URISyntaxException: Illegal character in path at index 25: http://user-api/api/user/{id}

源碼分析

feign.Target

注:url成員值為@FeignClient配置的path屬性值

public interface Target<T> {
?? ?@Override
? ? public Request apply(RequestTemplate input) {
? ? ? if (input.url().indexOf("http") != 0) {
? ? ? ? input.target(url());
? ? ? }
? ? ? return input.request();
? ? }
}

feign.RequestTemplate

注:此處將path屬性值直接解析為URI對(duì)象,如果包含形如{PathVariable}的路徑變量,會(huì)導(dǎo)致解析異常

public final class RequestTemplate implements Serializable {
? public RequestTemplate target(String target) {
? ? /* target can be empty */
? ? if (Util.isBlank(target)) {
? ? ? return this;
? ? }
? ? /* verify that the target contains the scheme, host and port */
? ? if (!UriUtils.isAbsolute(target)) {
? ? ? throw new IllegalArgumentException("target values must be absolute.");
? ? }
? ? if (target.endsWith("/")) {
? ? ? target = target.substring(0, target.length() - 1);
? ? }
? ? try {
? ? ? /* parse the target */?
? ? ? // 此處直接將path
? ? ? URI targetUri = URI.create(target);
? ? ? if (Util.isNotBlank(targetUri.getRawQuery())) {
? ? ? ? /*
? ? ? ? ?* target has a query string, we need to make sure that they are recorded as queries
? ? ? ? ?*/
? ? ? ? this.extractQueryTemplates(targetUri.getRawQuery(), true);
? ? ? }
? ? ? /* strip the query string */
? ? ? this.target = targetUri.getScheme() + "://" + targetUri.getAuthority() + targetUri.getPath();
? ? ? if (targetUri.getFragment() != null) {
? ? ? ? this.fragment = "#" + targetUri.getFragment();
? ? ? }
? ? } catch (IllegalArgumentException iae) {
? ? ? /* the uri provided is not a valid one, we can't continue */
? ? ? throw new IllegalArgumentException("Target is not a valid URI.", iae);
? ? }
? ? return this;
? }
}

解決辦法

如需使用路徑變量使用@RequestMapping代替Path

@FeignClient(name = "user-api")
@RequestMapping("/api/user/{id}")

@FeignClient使用詳解

@FeignClient標(biāo)簽的常用屬性如下

  • name:指定FeignClient的名稱(chēng),如果項(xiàng)目使用了Ribbon,name屬性會(huì)作為微服務(wù)的名稱(chēng),用于服務(wù)發(fā)現(xiàn)
  • url: url一般用于調(diào)試,可以手動(dòng)指定@FeignClient調(diào)用的地址
  • decode404:當(dāng)發(fā)生http 404錯(cuò)誤時(shí),如果該字段位true,會(huì)調(diào)用decoder進(jìn)行解碼,否則拋出FeignException
  • configuration: Feign配置類(lèi),可以自定義Feign的Encoder、Decoder、LogLevel、Contract
  • fallback: 定義容錯(cuò)的處理類(lèi),當(dāng)調(diào)用遠(yuǎn)程接口失敗或超時(shí)時(shí),會(huì)調(diào)用對(duì)應(yīng)接口的容錯(cuò)邏輯,fallback指定的類(lèi)必須實(shí)現(xiàn)@FeignClient標(biāo)記的接口
  • fallbackFactory: 工廠類(lèi),用于生成fallback類(lèi)示例,通過(guò)這個(gè)屬性我們可以實(shí)現(xiàn)每個(gè)接口通用的容錯(cuò)邏輯,減少重復(fù)的代碼
  • path: 定義當(dāng)前FeignClient的統(tǒng)一前綴,當(dāng)我們項(xiàng)目中配置了server.context-path,server.servlet-path時(shí)使用

1.首先

我們?cè)趩?dòng)類(lèi)里面加入注解,聲明開(kāi)啟Feign的遠(yuǎn)程調(diào)用,如下:

@EnableEurekaClient
@SpringBootApplication
@EnableFeignClients
public class LoginStart {
?? ?public static void main(String[] args) {
?? ??? ?SpringApplication.run(LoginStart.class, args);
?? ?}
}

2.編寫(xiě)接口類(lèi)

value="/xxx/xxx"就是我們服務(wù)方暴露的接口地址,如下:

import java.util.List;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
?
@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {
?? ?@RequestMapping(value="/custorm/getTest",method = RequestMethod.POST)
? ? List<String> test(@RequestParam("names") String[] names);
}

3.編寫(xiě)熔斷類(lèi)

發(fā)生錯(cuò)誤時(shí)回調(diào):

import java.util.List;
import org.springframework.stereotype.Component;
@Component
public class Hysitx implements IRemoteCallService{
?? ?@Override
?? ?public List<String> test(String[] names) {
?? ??? ?System.out.println("接口調(diào)用失敗");
?? ??? ?return null;
?? ?}
}

4.然后我們準(zhǔn)備兩個(gè)消費(fèi)者工程

custorm(服務(wù)方),login(調(diào)用方),然后在login的controller中寫(xiě)前臺(tái)調(diào)用接口:

@Autowired
private IRemoteCallService remot;
?? ?
@RequestMapping("/config")
public String config() {
?? ?String[] names = {"王五","張柳"};
?? ?return remot.test(names).toString();
}

5.然后在custorm工程中寫(xiě)一個(gè)接口

在這個(gè)接口里我們只將傳輸進(jìn)來(lái)的數(shù)據(jù)再添加一個(gè)數(shù)據(jù)返回回去

@RestController
@RequestMapping("/custorm")
public class CustormController {
?? ?
? ? @RequestMapping("/getTest")
? ? public List<String> Test(String[] names) {
? ? ?? ?List<String> name = new ArrayList<String>(Arrays.asList(names));
? ? ?? ?name.add("王麻子");
? ? ?? ?return name;
? ? }
}

6.然后我們啟動(dòng)注冊(cè)中心

配置中心以及兩個(gè)消費(fèi)者服務(wù),需要了解配置中心和注冊(cè)中心的搭建可以看我前兩篇文章,啟動(dòng)后瀏覽器我們進(jìn)行訪問(wèn)

可以看到,返回的數(shù)據(jù)中已經(jīng)包含了custorm工程中拼接的數(shù)據(jù),說(shuō)明我們遠(yuǎn)程調(diào)用接口成功,以上就是feign的簡(jiǎn)單使用

另外補(bǔ)充一些面試中長(zhǎng)問(wèn)的如何給@FeignClient添加Header信息

1.在@RequestMapping中添加,如下:

@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {
	@RequestMapping(value="/custorm/getTest",method = RequestMethod.POST,
		headers = {"Content-Type=application/json;charset=UTF-8"})
    List<String> test(@RequestParam("names") String[] names);
}

2.在方法參數(shù)前面添加@RequestHeader注解,如下:

@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {
	@RequestMapping(value="/custorm/getTest",method = RequestMethod.POST,
		headers = {"Content-Type=application/json;charset=UTF-8"})
    List<String> test(@RequestParam("names")@RequestHeader("Authorization") String[] names);
}

設(shè)置多個(gè)屬性時(shí),可以使用Map,如下:

import java.util.List;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
 
@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {
	@RequestMapping(value="/custorm/getTest",method = RequestMethod.POST,
		headers = {"Content-Type=application/json;charset=UTF-8"})
    List<String> test(@RequestParam("names") String[] names, @RequestHeader MultiValueMap<String, String> headers);
}

3.使用@Header注解,如下:

@FeignClient(name="custorm",fallback=Hysitx.class)
public interface IRemoteCallService {
	@RequestMapping(value="/custorm/getTest",method = RequestMethod.POST)
	@Headers({"Content-Type: application/json;charset=UTF-8"})
    List<String> test(@RequestParam("names") String[] names);
}

4.實(shí)現(xiàn)RequestInterceptor接口,如下:

@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {
 
    @Override
    public void apply(RequestTemplate temp) {
        temp.header(HttpHeaders.AUTHORIZATION, "XXXXX");
    }
 
}

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

相關(guān)文章

  • Java的分支結(jié)構(gòu)與循環(huán)你知道多少

    Java的分支結(jié)構(gòu)與循環(huán)你知道多少

    這篇文章主要為大家詳細(xì)介紹了Java的分支結(jié)構(gòu)與循環(huán),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-02-02
  • 淺談Java并發(fā)編程之Lock鎖和條件變量

    淺談Java并發(fā)編程之Lock鎖和條件變量

    這篇文章主要介紹了淺談Java并發(fā)編程之Lock鎖和條件變量,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • 通過(guò)xml配置SpringMVC注解DispatcherServlet初始化過(guò)程解析

    通過(guò)xml配置SpringMVC注解DispatcherServlet初始化過(guò)程解析

    這篇文章主要為大家介紹了通過(guò)xml配置SpringMVC注解DispatcherServlet初始化過(guò)程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-10-10
  • 理解JPA注解@GeneratedValue的使用方法

    理解JPA注解@GeneratedValue的使用方法

    這篇文章主要介紹了理解JPA注解@GeneratedValue的使用方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07
  • jpa?EntityManager?復(fù)雜查詢(xún)實(shí)例

    jpa?EntityManager?復(fù)雜查詢(xún)實(shí)例

    這篇文章主要介紹了jpa?EntityManager?復(fù)雜查詢(xún)實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • springboot中poi使用操作方法

    springboot中poi使用操作方法

    在項(xiàng)目中,有很多對(duì)excel的操作,大都數(shù)時(shí)候我們都會(huì)使用poi工具類(lèi),本文將介紹poi的一些使用方法,感興趣的朋友跟隨小編一起看看吧
    2023-08-08
  • 淺談Java鎖的膨脹過(guò)程以及一致性哈希對(duì)鎖膨脹的影響

    淺談Java鎖的膨脹過(guò)程以及一致性哈希對(duì)鎖膨脹的影響

    本文主要介紹了Java鎖的膨脹過(guò)程以及一致性哈希對(duì)鎖膨脹的影響,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • java web SpringMVC后端傳json數(shù)據(jù)到前端頁(yè)面實(shí)例代碼

    java web SpringMVC后端傳json數(shù)據(jù)到前端頁(yè)面實(shí)例代碼

    本篇文章主要介紹了java web SpringMVC后端傳json數(shù)據(jù)到前端頁(yè)面實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-03-03
  • mybatis-plus批量插入優(yōu)化方式

    mybatis-plus批量插入優(yōu)化方式

    MyBatis-Plus的saveBatch()方法默認(rèn)是單條插入,通過(guò)在JDBC URL添加rewriteBatchedStatements=true參數(shù)啟用批量插入,官方提供的sql注入器可自定義方法,如InsertBatchSomeColumn實(shí)現(xiàn)真批量插入,但存在單次插入數(shù)據(jù)量過(guò)大問(wèn)題,可通過(guò)分批插入優(yōu)化,避免超出MySQL限制
    2024-09-09
  • Java Spring之@Async原理案例詳解

    Java Spring之@Async原理案例詳解

    這篇文章主要介紹了Java Spring之@Async原理案例詳解,本篇文章通過(guò)簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-09-09

最新評(píng)論

汉川市| 当雄县| 宁津县| 海盐县| 乌审旗| 时尚| 浦江县| 离岛区| 宁国市| 察雅县| 东莞市| 大悟县| 新郑市| 建瓯市| 繁峙县| 徐水县| 武宁县| 介休市| 龙井市| 白玉县| 博兴县| 博爱县| 突泉县| 黎平县| 彭山县| 武川县| 博客| 卓资县| 遵义县| 蓬溪县| 泽州县| 志丹县| 永福县| 新沂市| 滦平县| 民县| 广河县| 新巴尔虎右旗| 雷山县| 扬州市| 恩施市|