我正在学习SpringBoot2.0用Java8.
我遵循了一些博客制作教程示例.
教程源代码是:
@GetMapping("/{id}/edit")
public String edit(@PathVariable Long id, Model model) {
model.addAttribute("categoryDto", categoryService.findOne(id));
return "category/edit";
}
Run Code Online (Sandbox Code Playgroud)
但是这段代码抛出了这个错误:
categoryService.findOne(ID)
我正在考虑将JPA findOne()方法更改为Optional< S >
怎么解决?
更多信息:
这是categoryService方法:
public Category findOne(Long id) {
return categoryRepository.findOne(id);
}
Run Code Online (Sandbox Code Playgroud) 我有一个Spring启动应用程序.
我收到以下错误
org.springframework.beans.factory.BeanCreationException:创建名为'birthdayController'的bean时出错:注入自动连接的依赖项失败; 嵌套异常是org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private com.esri.birthdays.dao.BirthdayRepository com.esri.birthdays.controller.BirthdayController.repository; 嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到[com.esri.birthdays.dao.BirthdayRepository]类型的限定bean用于依赖:预期至少有1个bean符合此依赖关系的autowire候选者.依赖注释:{@ org.springframework.beans.factory.annotation.Autowired(required = true)}在org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)〜[spring-beans-4.2 .4.RELEASE.jar:4.2.4.RELEASE]在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)〜[spring-beans-4.2.4.RELEASE.jar:4.2. 4.RELEASE] org.springframework上的org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE].在org.springframework.beans.factory.support.AbstractBeanFactory $ 1中的beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE]. getObject(AbstractBeanFactory.java:306)〜[spring-beans-4.2.4.RELEASE.jar:4.2.4.RELEASE] at或
以下是我的Repository类的代码
package com.esri.birthdays.dao;
import com.esri.birthdays.model.BirthDay;
public interface BirthdayRepository extends MongoRepository<BirthDay,String> {
public BirthDay findByFirstName(String firstName);
}
Run Code Online (Sandbox Code Playgroud)
以下是控制器.
package com.esri.birthdays.controller;
@RestController
public class BirthdayController {
@Autowired
private BirthdayRepository repository;
Run Code Online (Sandbox Code Playgroud)
如果它们在同一包装中则起作用. 不知道为什么