NekoBox 是一个基于 Spring Boot 的轻量级 API 终端开发框架,常用于快速构建 API 或后台管理系统,以下是配置 NekoBox 的基本方法和注意事项:


安装 NekoBox

需要通过 Maven 或 Gradle 将 NekoBox 引入项目中。

<dependency>
    <groupId>com.hxh</groupId>
    <artifactId>neko-box</artifactId>
    <version> latest version</version>
</dependency>

将 latest version 替换为最新版本号,可以在 NekoBox GitHub 中查找。


配置 Spring Boot 应用

创建一个 Spring Boot 主类,通常命名为 MainApplication 或 Application。

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}

配置 NekoBox

NekoBox 的核心配置通常在 application.properties 或 application.yml 中进行。

1 数据库配置

如果需要数据库支持,可以在 application.properties 中添加数据库配置:

spring.datasource.username=root
spring.datasource.password=123456
# 如果需要 Hikari 连接池,可以在 application.properties 中配置
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.maximumPoolSize=5

2 Redis 配置

如果需要 Redis 支持,可以在 application.properties 中添加 Redis 配置:

# Redis 配置
spring.redis.host=127...1
spring.redis.port=6379
spring.redis.timeout=300

3 JWT 配置

如果需要 JWT 支持,可以在 application.properties 中添加 JWT 配置:

# JWT 配置
neko.jwt.enable=true
neko.jwt.secret=your-secret-key

4 权限配置

如果需要权限管理,可以在 application.properties 中添加权限相关配置:

# 权限配置
neko.permission.enable=true
neko.permission.create_api=true

编写 Controller 和 Service

NekoBox 提供了丰富的注解和工具类,用于快速开发 API 或后台管理系统。

1 Controller

在 @RestController 上注解,使用 @GetMapping、@PostMapping、@PutMapping 等注解来定义 API 接口。

@RestController
public class UserController {
    @GetMapping("/api/users")
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }
    @PostMapping("/api/users/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }
}

2 Service

在 @Service 注解下,实现业务逻辑。

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }
    public User getUserById(Long id) {
        return userRepository.findById(id).orElseThrow(() -> new RuntimeException("用户不存在"));
    }
}

启动应用

在项目根目录中执行:

mvn spring-boot:run

测试 API

访问 http://localhost:808/api/users 可以查看所有用户列表。


常见问题

  1. 依赖冲突:如果有其他依赖冲突,可以尝试清理 Maven 项目(mvn clean)。
  2. 数据库连接错误:检查数据库配置是否正确,确保数据库服务运行。
  3. Redis 连接问题:确保 Redis 服务运行,并检查 Redis 连接地址和端口是否正确。
  4. JWT Secret 错误:JWT Secret 错误会导致令牌生成失败,确保 Secret 配置正确。

示例项目

以下是一个简单的 NekoBox 示例项目:

@SpringBootApplication
public class MainApplication {
    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/api/users")
    public List<User> getAllUsers() {
        return userService.getAllUsers();
    }
    @PostMapping("/api/users/{id}")
    public User getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }
}
@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;
    public List<User> getAllUsers() {
        return userRepository.findAll();
    }
    public User getUserById(Long id) {
        return userRepository.findById(id).orElseThrow(() -> new RuntimeException("用户不存在"));
    }
}
@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String username;
    private String password;
    private String email;
    public User(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;
    }
}
@Repository
public class UserRepository extends JpaRepository<User, Long> {
}

数据库配置

扫码添加小飞机VPN网络工具官方微信

扫码添加小飞机VPN网络工具官方微信

400-638-2751
扫码添加小飞机VPN网络工具官方微信

扫码添加小飞机VPN网络工具官方微信

网站地图