eskysky
Published on 2025-03-27 / 28 Visits
0

JeecgBoot后端项目新建module模块

JeecgBoot后端项目默认有三个模块,jeecg-boot-base-core、jeecg-module-demo和jeecg-module-system。

我们在实现业务时可以新增一个模块,将自己的业务代码与JeecgBoot的代码分离。

本文以JeecgBoot 3.7.3为例,新增名为“jeecg-module-abc”的模块。

新建模块

首先,在顶层maven项目中新建名为“jeecg-module-abc”的模块。

新建完成后检查顶层pom.xml文件中是否已包含该模块:

<modules>
        <module>jeecg-boot-base-core</module>
        <module>jeecg-module-demo</module>
        <module>jeecg-module-system</module>
        <module>jeecg-module-abc</module>
</modules>

关联依赖

在新建模块jeecg-module-abc的pom.xml文件中引入jeecg-boot-base-core和jeecg-module-system的依赖:

<dependencies>
        <dependency>
            <groupId>org.jeecgframework.boot</groupId>
            <artifactId>jeecg-boot-base-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jeecgframework.boot</groupId>
            <artifactId>jeecg-system-biz</artifactId>
        </dependency>
</dependencies>

在jeecg-system-start模块(在jeecg-module-system模块下)的pom.xml文件中引入jeecg-module-abc的依赖:

<dependency>
            <groupId>org.jeecgframework.boot</groupId>
            <artifactId>jeecg-module-abc</artifactId>
            <version>${jeecgboot.version}</version>
</dependency>

测试接口

在新建的jeecg-module-abc模块下创建一个测试Controller,路径为org.jeecg.modules.abc.TestController。

此处官方建议的包名规则为org.jeecg.modules.*,不按照此规则可能导致bean扫描不到。

TestController内容如下:

import org.jeecg.common.api.vo.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @GetMapping("/abc/test")
    public Result test(){
        return Result.OK("hello");
    }

}

现在可以启动项目测试一下接口,启动前最好重新clean和install下maven项目。

默认接口地址带jeecg-boot前缀,如http://127.0.0.1:8080/jeecg-boot/abc/test。

请求接口时需要在请求头携带Token,如果接口请求正常,将会返回类似下面的内容:

{
    "success": true,
    "message": "hello",
    "code": 200,
    "result": "hello",
    "timestamp": 1743048900000
}