SpringCloud使用feign遠程調用服務注入映射接口失敗問題


springCloud使用feign遠程調用服務注入映射接口失敗

在一次項目中,使用feign遠程調用服務時,發現feign的映射接口一直注入容器失敗

映射接口

package com.jn.feign.api;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient("RIBBON-PROVIDER")
@RequestMapping(value = "/provider")
public interface IProviderClient {
    @RequestMapping("/test1")
    String test1();
//
//    //方法接收多個參數
    @RequestMapping("/login")
    String login(@RequestParam("username") String username, @RequestParam("password") String password);
//
//    //方法接收對象
    @RequestMapping("/addUserFrom")
    String addUserFrom(User user);

//    //方法接收JSON數據
    @RequestMapping("/addUserJson")
    String addUserJson(@RequestBody User user);
//
//    //restful風格
    @RequestMapping("/getUserById/{id}")
    String getUserByID(@PathVariable Integer id);

}

主啟動類

@SpringBootApplication
@ComponentScan("com.jn")
@EnableEurekaClient
@EnableFeignClients("com.jn.feign.api")//開啟feign機制,並指定映射接口路徑
public class Day0111SpringcloudFeignConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(Day0111SpringcloudFeignConsumerApplication.class, args);
    }
}

調用的controller

@RestController
@RequestMapping("/feign")
public class FeignController {
    @Autowired
    IProviderClient client;
    @GetMapping("/test")
    public String test(){
        String s = client.test1();
        return s;
    }
}

啟動后報錯信息

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'feignController': Unsatisfied dependency expressed through field 'client'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.qf.feign.api.IProviderClient': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalStateException: PathVariable annotation was empty on param 0.

eureka服務器也確定RIBBON-PROVIDER已經注冊上去了,最終在檢查下發現了映射接口使用restful風格調用的時候@PathVariable沒有指定參數的id

修改后

在使用feign攜帶參數遠程調用服務的時候,一定要指定參數名,它不能像springMVC一樣將形參自動的作為參數值,需要自己手動指定


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM