SpringBoot入门(二):项目属性配置

项目属性配置

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();
    }
}
  • 多环境配置

三个配置文件,如下图

fPXky.png

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)"

fEUra.png

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() 等等
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值