考虑在配置中定义“包”类型的bean

时间:2018-08-13 07:51:56

标签: java spring rest spring-boot

我正在尝试在Spring Boot中提供一个简单的REST服务。在使用CrudRepository之前,一切工作正常。现在我收到此错误-

  

***申请无法开始

     
     

说明:

     

company.springBoot.io.Employee.EmployeeService中的字段er为必填项   'company.springBoot.io.Employee.EmployeeRepo'类型的Bean   找不到。

     

操作:

     

考虑定义一个类型的bean   配置中的“ company.springBoot.io.Employee.EmployeeRepo”。***

这是我的代码-

控制器-

@RestController
public class EmployeeController {


@Autowired
EmployeeService employeeService;

@GetMapping("/employees")
public List<Employee> getAllEmployees(){
    return employeeService.getAllEmployees();
}
@GetMapping("/employees/{id}")
public Employee getEmployee(@PathVariable int id) {
    return employeeService.getEmployee(id);
}

@PostMapping("/employees")
public String addEmployee(@RequestBody Employee e) {
    employeeService.addEmployee(e );
    return "Employee Records were added successfully";
}

@PutMapping("/employees/{id}")
public String updateEmployee(@RequestBody Employee e, @PathVariable int id) {
    return employeeService.updateEmployee(e, id);
    }

@DeleteMapping("/employees/{id}")
public @ResponseBody String deleteEmployee(@PathVariable int id) {
    return employeeService.deleteEmployee(id);

}

}

服务-

@Autowired
EmployeeRepo er;

public List<Employee> getAllEmployees() {


    List<Employee> list= new ArrayList<>();
    er.findAll()
    .forEach(list::add);
    return list;
        //return list;
}


public void addEmployee(Employee e) {
    // TODO Auto-generated method stub

    er.save(e);

}


public String updateEmployee(Employee e, int id) {
    // TODO Auto-generated method stub
er.save(e);
    String s= "The Employee with id  has been replaced by Employee with id "+e.getId();
    return s;
}


public String deleteEmployee(int id) {
    //  Auto-generated method stub
    er.deleteById(id);
    return "Employee withh id "+id+" has been removed from the company";
}


public Employee getEmployee(int id) {
    return er.findById(id).get();
}


}

CrudRepository接口

package company.springBoot.io.Employee;

import org.springframework.data.repository.CrudRepository;

public interface EmployeeRepo extends CrudRepository<Employee, Integer>{

}

package company.springBoot.io.EmployeeRest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages= 
{"company.springBoot.io.Employee"})
public class EmployeeRestApplication {

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

有人可以帮我吗?

-------------------------------------- 更新 -------------------------------------- 我这样更改了文件夹结构-enter image description here

现在我收到此错误-

enter image description here

1 个答案:

答案 0 :(得分:-1)

您缺少注释

package company.springBoot.io.Employee;

import org.springframework.data.repository.CrudRepository;
@Repository /// here is the trick
public interface EmployeeRepo extends CrudRepository<Employee, Integer>{

}

由于缺少注释,Spring没有扫描该接口,因此无法创建此类依赖项。