打开编辑器 创建一个新的项目

左边选择Spring Initializr 点击Next

选择打包方式 这里先选择jar包 后面会转成war包
其余都默认了 可以修改maven坐标

选择所需的组件 就是创建的pom初始化引入了选择的依赖

配置工程名 和 工程路径

工程目录展示

新建一个Controller

类名

使用@RestController注解交给spring管理 可以理解为 @Controller + @RsponseBody 的效果
@RequestMapping 映射路径为test

右上角点击启动项目

控制台输出 可以看到默认的端口为8080

在浏览器中访问

在resources目录下的application.properties中配置端口为80

重新启动项目访问 http://localhost/test 就可以看到效果
将项目改为war包并使用tomcat7插件来运行
到pom.xml中找到packaging修改为war


并添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

找到plugins节点添加tomcat插件
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<path>/</path>
<port>80</port>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>

配置启动方式

点击+号 选择maven

在Command line里面 写入 clean tomcat7:run

启动

成功输出

网友评论