springboot的Web项目编译运行时提示错误:Field userService in com.cetc.UserManger.controller.UserController require

错误描述:

  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);
    }
}

 

上一篇:spring事务什么时候会失效?


下一篇:.net增加webservice引用