一、介绍
它的目标是简化Spring应用和服务的创建、开发与部署,简化了配置文件,使用嵌入式web服务器,含有诸多开箱即用的微服务功能,可以和spring cloud联合部署。
Spring Boot的核心思想是约定大于配置,应用只需要很少的配置即可,简化了应用开发模式。
二、部署一个简单的Spring Boot项目实例
File-->new project:
只勾选web:
然后finish
创建项目完成:目录结构
pom相关的依赖:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
三、测试
1、简单例子(注意:@RestController相当于@Controller和@ResponseBody组合)
2、从application.yml文件获取值:
结果:
3、但是这样配置,当一个类属性过多时,就会显得异常繁琐,所以我们可以使用如下方式:
创建bean
package com.example.demo; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; /** * @Author: * @Date: 2019/5/9 * */ @Component @ConfigurationProperties(prefix = "boy") public class Boy { private String name; private String age; private String price; public String getName() { return name; } public Boy setName(String name) { this.name = name; return this; } public String getAge() { return age; } public Boy setAge(String age) { this.age = age; return this; } public String getPrice() { return price; } public Boy setPrice(String price) { this.price = price; return this; } }
在applcation.yml添加信息
@RestController public class HelloController { @Autowired private Boy boy; @RequestMapping(value = "/hello",method = RequestMethod.GET) public String say(){ return "名字:"+boy.getName()+",年龄:"+boy.getAge(); } }
4、多个配置文件调用
再application.yml中调用dev1配置文件
5、多个url访问同一个页面可以集合的方式,如下:
@RestController public class HelloController { @Autowired private Boy boy; @RequestMapping(value ={"/hello","/test"},method = RequestMethod.GET) public String say(){ return "名字:"+boy.getName()+",年龄:"+boy.getAge(); } }
6、获取url中的值
@RestController public class HelloController { @Autowired private Boy boy; @RequestMapping(value ={"/hello/{id}"},method = RequestMethod.GET) public String say(@PathVariable("id") String id){ return "名字:"+boy.getName()+",年龄:"+boy.getAge()+id; } }
需要注意的是这个id写在hello的前后都无所谓,都可以拿到值,只是访问的路径不同而已(如果是{id}/hello,那么就是2/hello)
当多个url访问同一个页面是,这种方式每一个url都需要去配置一个id
@RestController public class HelloController { @Autowired private Boy boy; @RequestMapping(value ={"/hello/{id}","/test/{id}"},method = RequestMethod.GET) public String say(@PathVariable("id") String id){ return "名字:"+boy.getName()+",年龄:"+boy.getAge()+",id:"+id; } }
可以使用这种方式,就不用在去每个url后面跟id了:
@RestController public class HelloController { @Autowired private Boy boy; @RequestMapping(value ={"/hello","/test"},method = RequestMethod.GET) public String say(@RequestParam("id") String id){ return "名字:"+boy.getName()+",年龄:"+boy.getAge()+",id:"+id; } }
7、组合注解@GetMapping 和RequestMapping功能是一样的
required表示参数是否必传,defaultValue代表默认值(String)