springboot的Web项目编译运行时提示错误:Field userService in com.cetc.UserManger.controller.UserController required a bean of type 'com.cetc.UserManger.service.UserService' that could not be found.

错误描述:

  springboot的Web项目编译运行时提示错误:Field userService in com.cetc.UserManger.controller.UserController required a bean of type 'com.cetc.UserManger.service.UserService' that could not be found.

解决办法:

  在提示错误的service的实现类impl上添加注解@Service。如下代码所示:

UserRepository的代码如下:
public interface UserRepository extends JpaRepository<User,Integer> {
}
UserService的代码如下:
public interface UserService {
    public Page<User> findAll(Pageable pageable);
}
UserServiceImpl的代码如下:
复制代码
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserRepository userRepository;

@Override
public Page<User> findAll(Pageable pageable){
return userRepository.findAll(pageable);
}
}
复制代码
UserController的代码如下:
复制代码
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/findAll/{page}/{size}")
    public Page<User> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        PageRequest request=PageRequest.of(page,size);//注意:page的索引是从0开始的,size的索引是从1开始的
        return userService.findAll(request);
    }
}
复制代码

 

posted @   rainbow70626  阅读(5795)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
历史上的今天:
2018-04-17 VC++中出现stack overflow错误时修改VC++的默认堆栈大小
点击右上角即可分享
微信分享提示