在使用openfeign遇到的注入失败
检查下后端通过地址是否能获取数据在后端通过地址可以获取到数据
但是在前端工程中却无法通过地址得到数据,一直报以下的错误
There was an unexpected error (type=Internal Server Error, status=500).
Error creating bean with name 'indexController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.slg.api.ContentClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
通过这个错可以知道是注入的问题,找到注入的地方,发现写了@Resouce
@Resource
ContentClient contentClient;
@RequestMapping({"/","/index"})
public String index(Model model){
getContents(model);
return "index";
}
public void getContents(Model model){
List<Content> lunbo_content = contentClient.getContentsByCategoryId(1);
model.addAttribute("lunbo_content",lunbo_content);
}
并且没有报错,于是跑到启动类中查看,发现注解使用错误
@SpringBootApplication
@EnableEurekaClient
@FeignClient
public class ProtalApp10250 {
public static void main(String[] args) {
System.out.println("前端展示启动10250");
SpringApplication.run(ProtalApp10250.class,args);
}
}
应该把注解@FeignClient
替换成@EnableFeignClients
再次启动就发现ok了