当前位置: 首页 > news >正文

以简单的例子从头开始建spring boot web多模块项目(一)

目的:从头梳理,如何手工从头建立多模块项目。
步骤:
1、建立maven项目,类型:maven Archetype,Name:ParentDemo
选择JDK版本,Archetype:org.apache.maven.archetypes:maven-archetype-quickstart
版本为1.1
在这里插入图片描述
2、在pom.xml文件中,修改Packaging为pom,groupId修改为需要的名称:
org.rainpet

<groupId>org.rainpet</groupId><artifactId>ParentDemo</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging>

3、为了做依赖管理,防止spring模块的版本冲突,添加dependencyManagement,添加spring-framework-bom包,这样子以后添加包时就不用指定版本了:

<dependencyManagement><dependencies><dependency><groupId>org.junit</groupId><artifactId>junit-bom</artifactId><version>5.10.2</version><scope>test</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-framework-bom</artifactId><version>5.3.33</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.7.12</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>io.spring.platform</groupId><artifactId>platform-bom</artifactId><version>2.0.1.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement>

4、删除父项目parentDemo中的src目录
5、新建-模块,name:webDemo1
语言:java
构建系统:Maven
Parent:parentDemo
在这里插入图片描述

6、子项目webDemo1中的pom.xml文件中,添加依赖包:
spring-boot-starter-web
spring-boot-starter

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency></dependencies>

7、webDemo1的pom.xml中添加build段

<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.6.13</version><configuration><mainClass>org.rainpet.Main</mainClass><layout>JAR</layout></configuration><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></build>

8、刷新项目,Main.java中,增加注解
@SpringBootApplication
main中,增加
SpringApplication.run(org.rainpet.Main.class,args);
即可以启动程序了,默认监听8080端口。

@SpringBootApplication
public class Main {public static void main(String[] args) {SpringApplication.run(org.rainpet.Main.class,args);System.out.println("Hello world!");}
}

9、在webDemo1项目的src中,确认是否有resources目录,没有的话,增加一个。
然后新建一个文件:application.properties
内容简单的加一个:

server.port= 8081

即可以更改监听端口
10、增加一个新包:org.rainpet.controller
11、controller中新增一个新类:DemoController
同时增加两个注解:
@Controller
@RestController
然后增加一个新类index,实现首页面访问即可

@Controller
@RestController
@RequestMapping("/demo")
public class DemoController {@ResponseBody@GetMapping("")public String index(){return "hello!";}
}

启动调试或者运行,即可看到效果:
在这里插入图片描述

12、也可以设置简单的get方法map,例:

@ResponseBody@GetMapping("name")public String name(@RequestParam(name="name",required = false,defaultValue = "张三")String name){return "hello "+name;}

在这里插入图片描述
13、简单的web后端程序已经可以跑起来了。

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 面向对象08:什么是多态
  • gping
  • sqli-labsSQL手工注入第26-30关
  • Android PopupWindow弹窗动态显示在View的上下方,
  • Bigtop 从0开始(上)
  • 从匿名内部类到Lambda表达式:Java编程的优雅进化
  • Challenge——spfa
  • 文件IO和多路复用IO
  • Flink入门(五)--Flink算子
  • 【北京迅为】《i.MX8MM嵌入式Linux开发指南》-第六篇 嵌入式GUI开发篇-第八十三章 Qt基础
  • Windows系统电脑安装多个Tomcat服务教程
  • 2021年高教社杯国赛a题 详细代码 文章 教学 2024数学建模国赛数模备赛: “FAST”主动反射面的形状调节
  • Android 自适应屏幕技术
  • SpringBootWeb 篇-深入了解 SpringBoot + Vue 的前后端分离项目部署上线与 Nginx 配置文件结构
  • HTML简单了解和基础知识记录
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • 2017届校招提前批面试回顾
  • ECMAScript入门(七)--Module语法
  • es6(二):字符串的扩展
  • extjs4学习之配置
  • javascript从右向左截取指定位数字符的3种方法
  • Laravel 菜鸟晋级之路
  • MySQL数据库运维之数据恢复
  • Ruby 2.x 源代码分析:扩展 概述
  • weex踩坑之旅第一弹 ~ 搭建具有入口文件的weex脚手架
  • 从tcpdump抓包看TCP/IP协议
  • 飞驰在Mesos的涡轮引擎上
  • 警报:线上事故之CountDownLatch的威力
  • 盘点那些不知名却常用的 Git 操作
  • 扫描识别控件Dynamic Web TWAIN v12.2发布,改进SSL证书
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • 智能网联汽车信息安全
  • 深度学习之轻量级神经网络在TWS蓝牙音频处理器上的部署
  • Nginx惊现漏洞 百万网站面临“拖库”风险
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • 浅谈sql中的in与not in,exists与not exists的区别
  • ​LeetCode解法汇总2583. 二叉树中的第 K 大层和
  • ​软考-高级-系统架构设计师教程(清华第2版)【第12章 信息系统架构设计理论与实践(P420~465)-思维导图】​
  • ​虚拟化系列介绍(十)
  • # Maven错误Error executing Maven
  • ## 1.3.Git命令
  • ### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
  • #使用清华镜像源 安装/更新 指定版本tensorflow
  • (1) caustics\
  • (17)Hive ——MR任务的map与reduce个数由什么决定?
  • (20050108)又读《平凡的世界》
  • (2024.6.23)最新版MAVEN的安装和配置教程(超详细)
  • (附源码)springboot 智能停车场系统 毕业设计065415
  • (附源码)springboot掌上博客系统 毕业设计063131
  • (原)本想说脏话,奈何已放下
  • (终章)[图像识别]13.OpenCV案例 自定义训练集分类器物体检测
  • ****** 二十三 ******、软设笔记【数据库】-数据操作-常用关系操作、关系运算
  • .net 获取某一天 在当月是 第几周 函数
  • .NET基础篇——反射的奥妙