项目属性配置
1.配置文件
- 默认为
application.properties
格式如下:
#默认为8080
server.port=8081
server.servlet.context-path=/firstboot
- 推荐使用
application.yml
格式如下
server:
port: 8080
servlet:
context-path: /firstboot
2.配置文件中获取数据
- 使用
@Value("${key}")
注解
yml中配置为
url: jdbc:mysql://localhost:3306/
username: root
password: 123456
测试类
@Value("${url}")
private String url;
@Value("${username}")
private String user;
@Value("${password}")
private String password;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(){
return url+" "+user+" "+password;
}
- 组合配置,针对value值过多的情况
yml中配置为
url: jdbc:mysql://localhost:3306/
username: root
password: 123456
content: "url: ${url},username: ${username},password: ${password}"
测试类
@RestController
public class HelloController {
@Value("${content}")
private String content;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(){
return content;
}
}
- 使用前缀获取多个Value值
yml配置为
一开始写的是DataSourceCon ,但是报错提示不能大写,不符合规范之类的,后来改成小写就可以了
datasourcecon:
url: jdbc:mysql://localhost:3306/
username: root
password: 123456
生成对应的类
@Component
@ConfigurationProperties(prefix = "datasourcecon")
public class DataSourceProp {
private String url;
private String username;
private String password;
...getter/setter...
}
需要添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
测试类
@RestController
public class HelloController {
@Autowired
private DataSourceProp dataSourceProp;
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(){
return dataSourceProp.getUrl()+" "+dataSourceProp.getUsername();
}
}
- 多环境配置
三个配置文件,如下图
application.yml
spring:
profiles:
#表示当前环境为dev
active: dev
application-dev.yml
server:
port: 8080
type: dev
application-prod.yml
erver:
port: 8080
type: prod
这样只切换主配置文件就可以了。
同时可以使用命令行选择参数:
mvn install
先打包java -jar target/firstboot-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev
3.Controller的使用
3.1 RestController
Spring4之后新加的注解,原来返回json需要@ResponseBody
配合@Controller
3.2 RequestMapping
- 配置url映射,常用的方法有
value
,method
value
可以设置成多地址映射,即使用集合的方式。
@RestController
public class HelloController {
@RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
public String say(){
return "Hello Spring Boot";
}
}
- 同时也可以给整个类加
@RequestMapping
,则访问路径为http://localhost:8080/springboot/hi
@RestController
@RequestMapping(value = "/springboot")
public class HelloController {
@RequestMapping(value = {"/hello","/hi"},method = RequestMethod.GET)
public String say(){
return "Hello Spring Boot";
}
}
3.3@PathVariable
- 获取url中的数据
@RequestMapping(value = "/hello/{id}",method = RequestMethod.GET)
public String say(@PathVariable("id") Integer myid){
return "id:"+myid;
}
- 变量的位置是可以变化的,比如
RequestMapping(value = "/{id}/hello)"
3.4 @RequestParam
- 获取请求参数的值
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(@RequestParam("id") Integer myid){
return "id:"+myid;
}
地址栏输入http://localhost:8080/hello?id=1000
即可
- 同时可以设置默认值,
required
表示值是否必须传入
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String say(@RequestParam(value = "id",defaultValue = "0",required = false) Integer myid){
return "id:"+myid;
}
地址栏输入http://localhost:8080/hello
,显示默认值为0
3.5@GetMapping
- 组合注解
- 可以使用
@GetMapping(value = "/hello")
代替@RequestMapping(value = "/hello",method = RequestMethod.GET)
- 同样还有
@PutMapping()
等等