jeecg-boot新建module模塊


jeecg-boot新建module模塊 

隨着jeecg-boot不斷拓展更新,愛好者越來越多,對於剛入門或者剛從事java(springboot)的人來說,會遇到各種各樣的問題。

今天就對jeecg-boot開源項目上的一個issues:373,寫個手冊-jeecg-boot下多模塊項目。

從issues:373 ,可以看出提問者,他想新建一個jeecg-boot-module-jm bundle,然后在 jeecg-boot-module-system 中依賴這個模塊。

前提條件:jeecg-boot-module-system 能成功編譯,並且jeecg-boot-module-system能成功啟動

(數據庫、redis配置正確)

jeecg-boot新建module模塊

新建maven項目

新建maven項目 取名jeecg-boot-module-jm

可以采用idea、myeclipse 等工具來新建一個maven項目

其中 pom.xml文件內容如下

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>jeecg-boot-module-jm</artifactId>
    <version>2.0.2</version>

    <parent>
        <groupId>org.jeecgframework.boot</groupId>
        <artifactId>jeecg-boot-parent</artifactId>
        <version>2.0.2</version>
    </parent>

    <repositories>
        <repository>
            <id>aliyun</id>
            <name>aliyun Repository</name>
            <url>http://maven.aliyun.com/nexus/content/groups/public</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
        <repository>
            <id>jeecg</id>
            <name>jeecg Repository</name>
            <url>http://maven.jeecg.org/nexus/content/repositories/jeecg</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    
    <dependencies>
        <dependency>
            <groupId>org.jeecgframework.boot</groupId>
            <artifactId>jeecg-boot-base-common</artifactId>
        </dependency>
    </dependencies>
    
    <!-- <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build> -->
</project>

 

注意:我這個pom文件直接復制了jeecg-boot-module-system 內容,將jeecg-boot-module-system名稱改為jeecg-boot-module-jm,注意注釋了

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

 

這段代碼,因為新建的項目要打包為jar在jeecg-boot-module-system引用,所以不需要把該項目打包一個springboot項目,注釋上面的代碼就可以了。

  • 創建業務包

在項目根目錄新建包名org.jeecg.modules.hello(以issues:373為例,也可以使用其他包名,記住這個包名,后面在接口問題swagger-ui使用到)

  • 添加業務(測試)代碼

(以issues:373為例,后面針對提出的問題,進行一一解答)這段代碼后面在swagegr-ui中訪問不到,因為方法上沒有添加@ApiOperation

package org.jeecg.modules.hello;

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

import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;

/**
 * 測試新的module
 * @author chengtg
 *
 */
@Slf4j
@Api(tags="新建module--jm")
@RestController
@RequestMapping("/hello")
public class HelloController  {
    @GetMapping(value="/")
    public Result<String> hello(){
        Result<String> result = new Result<String>();
        result.setResult("hello word!");
        result.setSuccess(true);
        return result;
    }
}

 

注意:我修改了注釋(@Api(tags="新建module--jm")),后面在swagegr-ui文檔用到

  • 將新建的項目納入parent中

將新建的jeecg-boot-module-jm 納入jeecg-boot-parent中

在jeecg-boot-framework項目中的pom文件 modules中添加<module>jeecg-boot-module-jm</module>

結果如下代碼

<modules>
    <module>jeecg-boot-base-common</module>
    <module>jeecg-boot-module-system</module>
    <module>jeecg-boot-module-jm</module>
</modules> 

 

添加項目依賴

在jeecg-boot-module-system項目中依賴 jeecg-boot-module-jms

修改jeecg-boot-module-system項目的pom文件

<dependencies>
        <dependency>
            <groupId>org.jeecgframework.boot</groupId>
            <artifactId>jeecg-boot-base-common</artifactId>
        </dependency>
        <dependency>
            <groupId>org.jeecgframework.boot</groupId>
            <artifactId>jeecg-boot-module-jm</artifactId>
            <version>2.0.2</version>
        </dependency>
</dependencies>

 

編譯整個jeecg-boot-framework

如果編譯如下:

[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ jeecg-boot-module-system --- [INFO] Installing /Users/chengtg/WS-platform/jeecg-boot-framework/jeecg-boot-module-system/target/jeecg-boot-module-system-2.0.2.jar to /Users/chengtg/.m2/repository/org/jeecgframework/boot/jeecg-boot-module-system/2.0.2/jeecg-boot-module-system-2.0.2.jar [INFO] Installing /Users/chengtg/WS-platform/jeecg-boot-framework/jeecg-boot-module-system/pom.xml to /Users/chengtg/.m2/repository/org/jeecgframework/boot/jeecg-boot-module-system/2.0.2/jeecg-boot-module-system-2.0.2.pom [INFO] ------------------------------------------------------------------------ [INFO] Reactor Summary for jeecg-boot-parent 2.0.2: [INFO] [INFO] jeecg-boot-parent .................................. SUCCESS [ 0.236 s] [INFO] jeecg-boot-base-common ............................. SUCCESS [ 1.143 s] [INFO] jeecg-boot-module-jm ............................... SUCCESS [ 1.066 s] [INFO] jeecg-boot-module-system ........................... SUCCESS [ 3.125 s] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 5.872 s [INFO] Finished at: 2019-08-04T21:41:09+08:00 [INFO] ------------------------------------------------------------------------ 

說明,新建項目jeecg-boot-module-jm並在jeecg-boot-module-system中依賴,成功!

  • 啟動jeecg-boot-module-system項目

  • 訪問接口文檔swagger

http://localhost:8080/jeecg-boot/doc.html

截圖如下:

問題來了:為什么新添加的HelloController中@Api(tags="新建module--jm")沒有 顯示

原因查看Swagger2Config配置,Swagger2Config.java

因為截圖中紅色圈出來的 部分

//加了ApiOperation注解的類,才生成接口文檔 .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) 

表示只有在controller類的方法上要添加ApiOperation注解的,否則是不生成swagegr-ui文檔

  • 修改HelloController代碼

修改jeecg-boot-module-system-jm項目HelloController類,給hello方法添加ApiOperation注解

package org.jeecg.modules.hello;

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

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * 測試新的module
 * @author chengtg
 *
 */
@Slf4j
@Api(tags="新建module--jm")
@RestController
@RequestMapping("/hello")
public class HelloController  {
    @ApiOperation("測試hello方法")
    @GetMapping(value="/")
    public Result<String> hello(){
        Result<String> result = new Result<String>();
        result.setResult("hello word!");
        result.setSuccess(true);
        return result;
    }
}

 

重新編譯啟動。

再次訪問接口文檔swagger:http://localhost:8080/jeecg-boot/doc.html

  • 奇跡出現了

  • 測試接口-調試

結果如上出現錯誤,因為被shiro攔截了

  • 去掉token攔截限制

如果不需要token可以訪問,可以在jeecg-boot-module-system項目中修改ShiroConfig.java配置類

代碼如下:

package org.jeecg.config;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.Filter;

import org.apache.shiro.mgt.DefaultSessionStorageEvaluator;
import org.apache.shiro.mgt.DefaultSubjectDAO;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.jeecg.modules.shiro.authc.ShiroRealm;
import org.jeecg.modules.shiro.authc.aop.JwtFilter;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

/**
 * @author: Scott
 * @date: 2018/2/7
 * @description: shiro 配置類
 */

@Configuration
public class ShiroConfig {
    
    /**
     * Filter Chain定義說明 
     * 
     * 1、一個URL可以配置多個Filter,使用逗號分隔
     * 2、當設置多個過濾器時,全部驗證通過,才視為通過
     * 3、部分過濾器可指定參數,如perms,roles
     */
    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        // 攔截器
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<String, String>();
        // 配置不會被攔截的鏈接 順序判斷
        filterChainDefinitionMap.put("/hello/**", "anon"); //測試新添加的module,不帶token訪問
        filterChainDefinitionMap.put("/sys/login", "anon"); //登錄接口排除
        filterChainDefinitionMap.put("/sys/getEncryptedString", "anon"); //獲取加密串
        filterChainDefinitionMap.put("/sys/sms", "anon");//短信驗證碼
        filterChainDefinitionMap.put("/sys/phoneLogin", "anon");//手機登錄        
        filterChainDefinitionMap.put("/sys/user/checkOnlyUser", "anon");//校驗用戶是否存在
        filterChainDefinitionMap.put("/sys/user/register", "anon");//用戶注冊
        filterChainDefinitionMap.put("/sys/user/querySysUser", "anon");//根據手機號獲取用戶信息
        filterChainDefinitionMap.put("/sys/user/phoneVerification", "anon");//用戶忘記密碼驗證手機號
        filterChainDefinitionMap.put("/sys/user/passwordChange", "anon");//用戶更改密碼
        filterChainDefinitionMap.put("/auth/2step-code", "anon");//登錄驗證碼
        filterChainDefinitionMap.put("/sys/common/view/**", "anon");//圖片預覽不限制token
        filterChainDefinitionMap.put("/sys/common/download/**", "anon");//文件下載不限制token
        filterChainDefinitionMap.put("/sys/common/pdf/**", "anon");//pdf預覽
        filterChainDefinitionMap.put("/generic/**", "anon");//pdf預覽需要文件
        filterChainDefinitionMap.put("/", "anon");
        filterChainDefinitionMap.put("/doc.html", "anon");
        filterChainDefinitionMap.put("/**/*.js", "anon");
        filterChainDefinitionMap.put("/**/*.css", "anon");
        filterChainDefinitionMap.put("/**/*.html", "anon");
        filterChainDefinitionMap.put("/**/*.svg", "anon");
        filterChainDefinitionMap.put("/**/*.jpg", "anon");
        filterChainDefinitionMap.put("/**/*.png", "anon");
        filterChainDefinitionMap.put("/**/*.ico", "anon");
        filterChainDefinitionMap.put("/druid/**", "anon");
        filterChainDefinitionMap.put("/swagger-ui.html", "anon");
        filterChainDefinitionMap.put("/swagger**/**", "anon");
        filterChainDefinitionMap.put("/webjars/**", "anon");
        filterChainDefinitionMap.put("/v2/**", "anon");
        
        //性能監控
        filterChainDefinitionMap.put("/actuator/metrics/**", "anon");
        filterChainDefinitionMap.put("/actuator/httptrace/**", "anon");
        filterChainDefinitionMap.put("/actuator/redis/**", "anon");
        
        //表單設計器
        filterChainDefinitionMap.put("/desform/**", "anon"); //自定義表單
        filterChainDefinitionMap.put("/test/jeecgDemo/demo3", "anon"); //模板測試
        filterChainDefinitionMap.put("/test/jeecgDemo/redisDemo/**", "anon"); //redis測試
        

        //流程模塊組件請求
        filterChainDefinitionMap.put("/act/process/**", "anon");
        filterChainDefinitionMap.put("/act/task/**", "anon");
        filterChainDefinitionMap.put("/act/model/**", "anon");
        filterChainDefinitionMap.put("/service/editor/**", "anon");
        filterChainDefinitionMap.put("/service/model/**", "anon");
        filterChainDefinitionMap.put("/service/model/**/save", "anon");
        filterChainDefinitionMap.put("/editor-app/**", "anon");
        filterChainDefinitionMap.put("/diagram-viewer/**", "anon");
        filterChainDefinitionMap.put("/modeler.html", "anon");
        filterChainDefinitionMap.put("/designer", "anon");
        filterChainDefinitionMap.put("/designer/**", "anon");
        filterChainDefinitionMap.put("/plug-in/**", "anon");
    
        //排除Online請求
        filterChainDefinitionMap.put("/auto/cgform/**", "anon");
        //FineReport報表
        filterChainDefinitionMap.put("/ReportServer**", "anon");
    
        // 添加自己的過濾器並且取名為jwt
        Map<String, Filter> filterMap = new HashMap<String, Filter>(1);
        filterMap.put("jwt", new JwtFilter());
        shiroFilterFactoryBean.setFilters(filterMap);
        // <!-- 過濾鏈定義,從上向下順序執行,一般將/**放在最為下邊
        filterChainDefinitionMap.put("/**", "jwt");

        // 未授權界面返回JSON
        shiroFilterFactoryBean.setUnauthorizedUrl("/sys/common/403");
        shiroFilterFactoryBean.setLoginUrl("/sys/common/403");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    @Bean("securityManager")
    public DefaultWebSecurityManager securityManager(ShiroRealm myRealm) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(myRealm);

        /*
         * 關閉shiro自帶的session,詳情見文檔
         * http://shiro.apache.org/session-management.html#SessionManagement-
         * StatelessApplications%28Sessionless%29
         */
        DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO();
        DefaultSessionStorageEvaluator defaultSessionStorageEvaluator = new DefaultSessionStorageEvaluator();
        defaultSessionStorageEvaluator.setSessionStorageEnabled(false);
        subjectDAO.setSessionStorageEvaluator(defaultSessionStorageEvaluator);
        securityManager.setSubjectDAO(subjectDAO);

        return securityManager;
    }

    /**
     * 下面的代碼是添加注解支持
     * @return
     */
    @Bean
    @DependsOn("lifecycleBeanPostProcessor")
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }

    @Bean
    public LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

}

 

編譯並重啟服務,

再次訪問接口文檔http://localhost:8080/jeecg-boot/doc.html

至此,新建一個module,且測試成功!

  • 回顧下主要步驟:

1.新建一個module maven項目,取名為jeecg-boot-module-jm,修改pom文件

controller中 類和方法都需要添加注解:其中類@Api(tags="新建module--jm"),方法@ApiOperation("測試hello方法")

2.將jeecg-boot-module-jm放入jeecg-boot-framework 的modules中

3.jeecg-boot-module-system依賴新添加的maven項目

4.如果不需要攔截,修改jeecg-boot-module-system配置類ShiroConfig.java

拓展:swagger-ui分組

如果想將新建的module 在swagger中以分組形式展現,可以參考如下配置Swagger2Config.java

編譯啟動,訪問swagger-ui文檔:http://localhost:8080/jeecg-boot/doc.html

如圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM