如何在JHipster Gateway中添加第二个前端应用并保留默认webapp?
在JHipster中实现双Web目录的方案
完全可以实现你提出的需求——保留默认的src/main/webapp供产品负责人(PO)、项目经理(PM)和测试人员(QA)使用Swagger及实体功能,同时新增webapp2作为主UI。以下是具体的实现思路:
1. 新增前端目录结构
- 在
src/main下创建webapp2目录,按照React项目的标准结构搭建主UI代码,包含组件、路由、状态管理等核心逻辑。 - 给
webapp2配置独立的package.json和构建文件(比如webpack.config.js或vite.config.js),保证能单独完成构建打包。
2. 调整后端静态资源映射
- 修改Spring Boot的配置类(比如
WebConfigurer),添加对webapp2目录的静态资源映射。示例代码:
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // 保留原webapp的资源映射 registry.addResourceHandler("/admin/**") .addResourceLocations("classpath:/static/", "classpath:/public/") .setCachePeriod(getCachePeriod()) .resourceChain(true) .addResolver(new PathResourceResolver() { @Override protected Resource getResource(String resourcePath, Resource location) throws IOException { Resource requestedResource = location.createRelative(resourcePath); return requestedResource.exists() && requestedResource.isReadable() ? requestedResource : new ClassPathResource("/static/index.html"); } }); // 添加webapp2的映射,将根路径指向主UI registry.addResourceHandler("/**") .addResourceLocations("classpath:/webapp2/") .setCachePeriod(getCachePeriod()); }
- 上面的配置将原
webapp的内容通过/admin前缀访问,根路径默认指向webapp2的主UI,你也可以根据需求调整路径规则。
3. 配置构建脚本
- 在项目根目录的
pom.xml或gradle.build中,添加webapp2的构建步骤,确保打包时将webapp2的构建产物复制到target/classes/webapp2目录(Spring Boot会自动识别classpath:下的资源)。 - 示例Maven配置:
<plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>exec-maven-plugin</artifactId> <executions> <execution> <id>build-webapp2</id> <phase>generate-resources</phase> <goals> <goal>exec</goal> </goals> <configuration> <workingDirectory>src/main/webapp2</workingDirectory> <executable>npm</executable> <arguments> <argument>run</argument> <argument>build</argument> </arguments> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <executions> <execution> <id>copy-webapp2-dist</id> <phase>process-resources</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${project.build.outputDirectory}/webapp2</outputDirectory> <resources> <resource> <directory>src/main/webapp2/build</directory> <filtering>false</filtering> </resource> </resources> </configuration> </execution> </executions> </plugin>
4. 验证功能访问
- 原
src/main/webapp中的Swagger UI和实体管理页面可以通过/admin/swagger-ui.html路径访问,确保相关角色能正常使用这些功能。 - 主UI则通过根路径直接访问,满足日常用户的使用需求。
通过以上步骤,就能同时维护两个Web目录,满足不同角色的使用场景。
内容的提问来源于stack exchange,提问作者Lazar Nikolic
相关产品推荐
相关产品推荐

