zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    C#中方法的分类、定义、调用(3)
    C#中的输入和输出与类和对象(2)
    .net中的数据类型与数据转换(1)
    android第二章控件2
    android第二章控件1
    安卓 第一章
    二进制文件的读写与小结
    字符流
    File类与字节流
    字节流
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/14672193.html
Copyright © 2011-2022 走看看