SpringBoot-02-之参数传递

一:url传参

1.get方式Url传参:@PathVariable
    ------get方式Url传参
    @GetMapping({"/id/{the_Param}"})
    public String id(@PathVariable("the_Param") Integer id) {
        return "id:" + id;
    }
9414344-ff9af7d796b8baa9.png
get方式Url传参
2.get方式Url传参:@RequestParam
    ------get方式Url传参
    @GetMapping(/user)
    public String username(@RequestParam("username") String str) {
        return "username is:" + str;
    }
9414344-9b992902c2e34312.png
get方式Url传参
3.get方式Url传参:@RequestParam+默认参数
    ------get方式Url传参+默认参数
    @GetMapping("/user_default")
    public String usernameWithDefaultParam(
            @RequestParam(value = "username", defaultValue = "toly1994")
                    String str) {
        return "username is:" + str;
    }
9414344-3f7138701c1ee04b.png
默认参数

二:配置文件传值使用

1.配置文件字段使用
src\main\resources\application-dev.yml
name: black magic
atk: 2500
desc: "name:${name} And atk:${atk}"
toly1994.com.toly01.controller.ParamController
  ------配置文件字段使用
    @Value("${name}")
    private String lever;
    @Value("${atk}")
    private int atk;
    @Value("${desc}")
    private String desc;

    @GetMapping("/YoGiOh")
    public String paramInRes() {
        return desc;
    }
9414344-c199df06160ac5be.png
配置文件字段使用
2.获取配置文件组成员
2-1:写入字段---src\main\resources\application-dev.yml
monster:
  name: blue eyes white Dragon
  atk: 3000
2-2:创建实体类---toly1994.com.toly01.properties.MonsterProperties
@Component //org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "monster")
public class MonsterProperties {
    private String name;
    private int atk;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAtk() {
        return atk;
    }

    public void setAtk(int atk) {
        this.atk = atk;
    }

    @Override
    public String toString() {
        return "MonsterProperties{" +
                "name='" + name + '\'' +
                ", atk=" + atk +
                '}';
    }
}
2-3:增加方法---toly1994.com.toly01.controller.ParamController
 -----获取配置文件组成员
    @RestController
    public class HelloSpringBoot {
        @Autowired //自动创建对象
        private MonsterProperties monster;

        @GetMapping("/monster")
        public String say() {
            return monster.toString();
        }
    }
9414344-fe2fc83c30393409.png
获取配置文件组成员

转载于:https://www.cnblogs.com/toly-top/p/9782003.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值