我正在尝试使用 spring boot 为一个项目做一个 API,当它抛出错误时我尝试编译:
Parameter 0 of constructor in HNFOrmations.ProjetFullStack.appController.TypeUserController required a bean of type 'HNFOrmations.ProjetFullStack.appServices.TypeUserService' that could not be found.
Action:
Consider defining a bean of type 'HNFOrmations.ProjetFullStack.appServices.TypeUserService' in your configuration."
但是我已经在上述位置创建了一个 TypeUserService:
TypeUserService 的代码
package HNFOrmations.ProjetFullStack.appServices;
import org.springframework.stereotype.Service;
import HNFOrmations.ProjetFullStack.Entity.*;
import java.util.List;
@Service
public interface TypeUserService {
List<TypeUser> findAll();
TypeUser findById(int id);
TypeUser save(TypeUser typeUser);
TypeUser deleteById(int id);
TypeUser findByType(String type);
}
控制器代码:
package HNFOrmations.ProjetFullStack.appController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;
import HNFOrmations.ProjetFullStack.Entity.TypeUser;
import HNFOrmations.ProjetFullStack.Services.TypeUserService;
import java.util.List;
import static org.springframework.http.HttpStatus.NOT_FOUND;
@RestController
@CrossOrigin(origins = "http://localhost:4200")
@RequestMapping("/type-users")
public class TypeUserController {
private HNFOrmations.ProjetFullStack.appServices.TypeUserService typeUserService;
@Autowired
public TypeUserController(HNFOrmations.ProjetFullStack.appServices.TypeUserService Service) {
this.typeUserService = Service;
}
@GetMapping("")
public List<TypeUser> findAll() {
return typeUserService.findAll();
}
@GetMapping("/{id}")
public TypeUser findById(@PathVariable int id) {
return typeUserService.findById(id);
}
@PostMapping("/create")
public TypeUser addTypeUser(@RequestBody TypeUser typeUser) {
return typeUserService.save(typeUser);
}
@PutMapping("/update")
public TypeUser updateTypeUser(@RequestBody TypeUser typeUser) {
int id = typeUser.getId();
TypeUser dbTypeUser = typeUserService.findById(id);
if (dbTypeUser == null) {
throw new ResponseStatusException(NOT_FOUND, "Unable to find typeUser with id : " + id);
}
return typeUserService.save(typeUser);
}
@DeleteMapping("/{id}")
public void deleteTypeUser(@PathVariable int id) {
typeUserService.deleteById(id);
}
}
我不明白这个错误来自哪里
首先感谢@Jens
答案很简单: 我不得不将 @Service 注释到所述服务的接口,而不是将其放入执行我没有执行的操作的类中,这为我解决了问题。
谢谢大家的评论