创建工程
maven配置
本地库 or 阿里云镜像;
第一个应用
创建Controller
//使用@RestController!!!
@RestController
public class HelloController {
}
创建方法
//使用@RequestMapping!!!
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String say(){
return "hello spring boot";
}
配置
application.properties语法
其中,context-path必须以/开头;
appliacation.yml语法(推荐使用)
server:
port: 8080
context-path: /girl
cupSize: B
age: 18
content: "cupSize: ${cupSize}, age: ${age}"
其中,:后必须跟着空格;
访问端口:8080
访问路径:http://localhost:8080/girl/hello
使用配置文件参数
单个参数
@RestController
public class HelloController {
//可配置文件中配置多个参数,这里引用多个;
@Value("${cupSize}")
private String cupSize;
@Value("${age}")
private Integer age;
@Value("${content}")
private String content;
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String say(){
//return cupSize;
return content;
}
}
多个有关联的参数(参数分组!!!)
//配置文件中:
server:
port: 8080
context-path: /girl
girl:
cupSize: B
age: 18
//新建类:
@Component //为了被调用
@ConfigurationProperties(prefix = "girl") //获取前缀是girl的配置
public class GirlProperties {
private String cupSize;
private Integer age;
public String getCupSize() {
return cupSize;
}
public void setCupSize(String cupSize) {
this.cupSize = cupSize;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
//controller中
@RestController
public class HelloController {
//调用配置参数类
@Autowired
private GirlProperties girlProperties;
@RequestMapping(value="/hello",method = RequestMethod.GET)
public String say(){
return girlProperties.getCupSize(); //使用配置参数
}
}
测试、生产环境使用不同配置
设置 测试、生产环境配置文件
设置要使用的配置文件