starter
SpringBoot中常见的项目名称,定义当前项目使用的所有依赖坐标,以达到减少依赖配置的目的。
就比如说web的依赖,只需要引入下面的一个依赖就可以了,这个依赖里面有很多小依赖。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
点进去之后会看到有很多的小依赖。
<?xml version="1.0" encoding="UTF-8"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <!-- This module was also published with a richer model, Gradle metadata, --> <!-- which should be used instead. Do not delete the following line which --> <!-- is to indicate to Gradle or any Gradle module metadata file consumer --> <!-- that they should prefer consuming it instead. --> <!-- do_not_remove: published-with-gradle-metadata --> <modelVersion>4.0.0</modelVersion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.6.3</version> <name>spring-boot-starter-web</name> <description>Starter for building web, including RESTful, applications using Spring MVC. Uses Tomcat as the default embedded container</description> <url>https://spring.io/projects/spring-boot</url> <organization> <name>Pivotal Software, Inc.</name> <url>https://spring.io</url> </organization> <licenses> <license> <name>Apache License, Version 2.0</name> <url>https://www.apache.org/licenses/LICENSE-2.0</url> </license> </licenses> <developers> <developer> <name>Pivotal</name> <email>info@pivotal.io</email> <organization>Pivotal Software, Inc.</organization> <organizationUrl>https://www.spring.io</organizationUrl> </developer> </developers> <scm> <connection>scm:git:git://github.com/spring-projects/spring-boot.git</connection> <developerConnection>scm:git:ssh://git@github.com/spring-projects/spring-boot.git</developerConnection> <url>https://github.com/spring-projects/spring-boot</url> </scm> <issueManagement> <system>GitHub</system> <url>https://github.com/spring-projects/spring-boot/issues</url> </issueManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.6.3</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-json</artifactId> <version>2.6.3</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.6.3</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.3.15</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.15</version> <scope>compile</scope> </dependency> </dependencies> </project>
parent
所有的SpringBoot项目都要继承的项目,定义了若干个坐标的版本号(依赖管理,而非依赖),已达到减少依赖冲突的目的。就是在下面协写依赖的时候,有时候可能没有版本号,其实本质上是SpringBoot已经收录了。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.3</version> </parent>
引导类
SpringBoot工程提供引导类来用于启动成程序,就是run这个引导类就可以启动SpringBoot项目了,本质上这个东西就是一个大的Spring容器,然后之后写的bean都会放到这个spring里面的。
tomcat服务器
内嵌Tomcat服务器是SpringBoot辅助功能之一。
创建好springboot项目之后,就可以开始写第一个SpringBoot项目了。
package com.itheima; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/books") public class BookController { @GetMapping("/news") public String getId() { System.out.println("SpringBoot is running"); return "SpringBoot is running"; } }
@RestController
@RestController = @ResponseBody + @Controller合在一起的作用
如果只是使用@RestController注解Controller,则Controller中的方法无法返回jsp页面,配置的视图解析器InternalResourceViewResolver不起作用,返回的内容就是Return 里的内容。
例如:本来应该到success.jsp页面的,则其显示success.
如果需要返回到指定页面,则需要用 @Controller配合视图解析器InternalResourceViewResolver才行。
如果需要返回JSON,XML或自定义mediaType内容到页面,则需要在对应的方法上加上@ResponseBody注解。(@Controller + @ResponseBody)
@RequestMapping
@GetMapping @PostMapping @PutMapping @DeleteMapping @PatchMapping @PathVariable
package com.itheima; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/books") public class BookController { @GetMapping("/news") public String getId() { System.out.println("SpringBoot is running"); return "SpringBoot is running"; } @GetMapping("{id}") public String getById(@PathVariable Integer id) { System.out.println(id); return id + ""; } }