REST API: org.springframework.beans.factory.UnsatisfiedDependencyException:

huangapple go评论78阅读模式
英文:

REST API: org.springframework.beans.factory.UnsatisfiedDependencyException:

问题

我正在构建一个 REST API,但似乎无法摆脱 org.springframework.beans.factory.UnsatisfiedDependencyException 错误,我在代码中找不到问题所在。我将会欣赏对此的新的洞察力。

附言:在这里使用了 mongo DB。

首先,完整的错误信息如下:

2020-05-04 01:16:31.468  WARN 14412 
--- [           main] ConfigServletWebServerApplicationContext : 
在上下文初始化过程中遇到异常 - 正在取消刷新尝试:
org.springframework.beans.factory.UnsatisfiedDependencyException:
创建 bean 时出错,bean 名称为 'productController':
通过字段 'productService' 表达的不满足的依赖关系;
嵌套异常是 org.springframework.beans.factory.UnsatisfiedDependencyException:
创建 bean 时出错,bean 名称为 'productServiceImpl':
通过字段 'productRepo' 表达的不满足的依赖关系;
嵌套异常是 org.springframework.beans.factory.BeanCreationException:
创建 bean 时出错,bean 名称为 'productRepo':
初始化方法的调用失败;嵌套异常是 org.springframework.data.mapping.PropertyReferenceException:
找不到类型为 Product 的属性 id!

public class Product {

    @Id
    private int ProductId;
    
// 其他变量以及设置和获取方法 
@Repository
public interface ProductRepo extends MongoRepository<Product, String> {

   // 方法
}

public interface ProductService {
 // 方法
}


@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRepo productRepo;
 
// 实现部分
}
英文:

I am building a REST API and can't seem to get rid of the org.springframework.beans.factory.UnsatisfiedDependencyException, I can't spot the issue in this. I will appreciate a fresh insight into this.

p.s: Using mongo DB for this

first off the full error

2020-05-04 01:16:31.468  WARN 14412 
--- [           main] ConfigServletWebServerApplicationContext : 
Exception encountered during context initialization - 
cancelling refresh attempt: 
org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name &#39;productController&#39;: 
Unsatisfied dependency expressed through field &#39;productService&#39;; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
 Error creating bean with name &#39;productServiceImpl&#39;:
 Unsatisfied dependency expressed through field &#39;productRepo&#39;; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name &#39;productRepo&#39;: 
Invocation of init method failed; nested exception is org.springframework.data.mapping.PropertyReferenceException:
 No property id found for type Product!

public class Product {

    @Id
    private int ProductId;
    
// and other variables plus setters and getters 
@Repository
public interface ProductRepo extends MongoRepository&lt;Product, String&gt; {

   // methods
}

public interface ProductService {
 // methods
}


@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductRepo productRepo;
 
// implementations
}

答案1

得分: 2

尝试按照以下方式更改 Product 类:

@Entity
public class Product {

    @Id
    private String productId; // MongoRepository<Product, String>;

    // 方法
}

Product 类中,缺少 @Entity 注解,并且 productId 应该是一个 String,正如存储库中指定的一样。

或者,如果您想保持当前字段类型,可以更改为 extends MongoRepository<Product, Integer>

无论如何,它们应该匹配。

英文:

Try changing the Product class as the following:

@Entity
public class Product {

    @Id
    private String productId; // MongoRepository&lt;Product, String&gt;

    // methods
}

In the Product class the @Entity annotation is missing and the productId should be a String, as specified in the repository.

Or change to extends MongoRepository&lt;Product, Integer&gt; if you want to keep the current field type.

In any case, they should match.

答案2

得分: 0

确保你的 Spring 应用程序知道你的类位于哪里。
使用 @ComponentScan(basePackages = "com.package")

另外,如果你的产品类是希望持久化的,请使用 @Entity 进行标记,这样 Spring 就会知道它并且会将其创建为一个 Bean。

@Service 标记你的接口 ProductService,这样 Spring 就会知道它的存在。

英文:

Make You That Your Spring Application Knows Where Your Classes Resides.
Use @ComponentScan(basePackages = &quot;com.package&quot;).

Moreover If Your Product Class Is Something That You Want to Persist Mark It With @Entity So Spring Knows about It And Creates A Bean Out Of it.

Mark Your interface ProductService with @service, So that Spring Knows About it.

答案3

得分: 0

UnsatisfiedDependencyException:
Repository.java这个页面中,类似于@respository注解的类作为返回类型为null,而在optinal的附近使用了default关键字。
这个方法对我有用。

英文:

Unsatisfieddependencyexception:
Repository.java this page like @respository annotation class below being return type null and near optinal <> using default keyword
This method working me.

huangapple
  • 本文由 发表于 2020年5月4日 03:50:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61580731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定