目录

SpringBoot引入的依赖是起步依赖,是一个功能引入一个依赖,而不是一个jar包引入一个依赖。
点击上面的生成按钮之后,会自动下载一个官网帮我们搭建的SpringBoot项目的压缩包,解压之后,把解压的文件导入到idea中即可。
之前学习的时候,普通java程序打的是一个jar包,而web程序打得是一个war包,而这是一个Spring Web项目,但是为什么打得是一个jar包呢,请看下面的打包类型说明:


- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.7.4version>
- <relativePath/>
- parent>
这种引入方式使得不必再像之前一样引入那么多依赖,而是现在引入很少的东西即可。
- <dependencies>
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- dependency>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-testartifactId>
- <scope>testscope>
- dependency>
- dependencies>
3.spring-boot-maven-plugin插件是将项目打包成jar包的插件。
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
如果发生报错(spring-boot-maven-plugin呈红色),请参考这篇文章,我的是添加了个版本号就好了。
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class Springbootdemo2Application {
-
- public static void main(String[] args) {
-
- SpringApplication.run(Springbootdemo2Application.class, args);
- }
-
- }

在pom中添加项目的父工程、起步依赖、插件
-
- <parent>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-parentartifactId>
- <version>2.7.4version>
- parent>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-webartifactId>
- <version>2.7.4version>
- dependency>
- dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-maven-pluginartifactId>
- plugin>
- plugins>
- build>
编写启动类
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
-
- @SpringBootApplication
- public class SpringBootAPP {
- public static void main(String[] args) {
- SpringApplication.run(SpringBootAPP.class,args);
- }
- }
- #日志格式
- logging.pattern.console=%d{MM/dd HH:mm:ss.SSS} %clr(%-5level) --- [%-15thread] %cyan(%-50logger{50}):%msg%n
- #端口号
- server.port=8888
项目结构:

运行启动类主方法,启动项目
刷新一下maven,上方就会自动出现服务器

点击绿色按钮运行即可。
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.ResponseBody;
-
- @Controller
- public class MyController {
- @RequestMapping("/hello")
- @ResponseBody
- public String hello(){
- System.out.println("hello springboot!");
- return "hello springboot!";
- }
- }
在上面的项目结构下,在first下创建一个controller包,再在controller包下新建一个MyController类


如果前面学的不熟的话,可能看到这里会有一个疑问,当控制类的方法返回值是String类型时,那么返回值不是和视图解析器的前缀和后缀组成了要跳转的页面路径吗???之前确实是那样的,但是这个测试方法上加了@ResponseBody注解,通过看下面的注解解释可知,此注解将返回的对象转换成JSON的格式直接写入数据流当中了,而且使用此注解也不会经过视图解析器。所以直接呈现到了web页面当中。
@ResponseBody、@RestController将方法返回的对象转为JSON格式。
作用:方法返回的对象转换为 JSON 格式,并将 JSON 数据直接写入到输出流中,使用此注解后不会再经过视图解析器。使用该注解可 以处理 Ajax 请求。
位置:方法上方或方法返回值前。