Feign消费服务,程序入口类HiController启动时始终无法注入scheduleServiceHi,报service could not be found

1、Feign消费服务,程序入口类HiController启动时始终无法注入scheduleServiceHi。报错如下:

Description:
Field scheduleServiceHi in com.example.servicefeign.controller.HiController required a bean of type 'com.example.servicefeign.service.ScheduleServiceHi' that could not be found.
The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.example.servicefeign.service.ScheduleServiceHi' in your configuration.

程序入口类HiController代码如下:

package com.example.servicefeign.controller;

import com.example.servicefeign.service.ScheduleServiceHi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HiController {
    @Autowired
    private ScheduleServiceHi scheduleServiceHi;
    @RequestMapping(value = "/hi")
    public String sayHi(@RequestParam String name){
       return scheduleServiceHi.sayHiFromClientOne(name);
      // service-hi服务中默认的方法
      //  return scheduleServiceHi.home();
    }
}

被调用接口ScheduleServiceHi 代码如下:

package com.example.servicefeign.service;

import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "service-hi")
//@EnableFeignClients(value ="service-hi" )
public interface ScheduleServiceHi {
    @RequestMapping(value = "/Hi",method = RequestMethod.GET)

    String sayHiFromClientOne(@RequestParam(value = "name") String name);
    //service-hi服务中默认的方法
//    String home();
}

排查了下才发觉是,@FeignClient 与@EnableFeignClients两个注解搞错了。应该使用@FeignClient。但却错误的使用了@EnableFeignClients。特此记录之。

2、被调用接口ScheduleServiceHi 中
String sayHiFromClientOne(@RequestParam(value = "name") String name);
为追加的方法。而String home();为服务提供方service-hi定义的方法。程序入口类HiController调用两者均可成功消费服务。

服务提供方service-hi代码:

package com.test.example.eurekaclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableEurekaClient
public class EurekaClientServiceHiApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaClientServiceHiApplication.class, args);
    }
    @Value("${server.port}")
    String port;
    @RequestMapping(value = "/Hi")
    public String home (@RequestParam(value = "name",defaultValue = "Kobe")String name){
        return  "Hi," + name+"I'm from port:"+ port;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值