首页 > 解决方案 > Eclipse Maven Rest Api:错误:无法将 UserNotFoundException 解析为类型。如何解决这个问题?

问题描述

这是完整的代码。它在 UserNotFoundException 处引发错误。

@RestController
public class UserResource {
    
    //GET /users/{id}
    //retrieveUser(int id)
    @GetMapping("/users/{id}")
    public User retrieveUser(@PathVariable int id) {
        User user = service.findone(id);
        if(user==null)
            throw new UserNotFoundException("id-"+ id); //this throwing the error.
        return user;
    }
    
}

标签: javaeclipsespring-bootmaven

解决方案


UserNotFoundException听起来像是您自己定义的异常。所以我想你必须导入它:

import yourpackage.UserNotFoundException

推荐阅读