美文网首页SpringBoot之旅IT技术篇
SpringBoot-02-之参数传递

SpringBoot-02-之参数传递

作者: e4e52c116681 | 来源:发表于2018-07-16 09:55 被阅读5次

一:url传参

1.get方式Url传参:@PathVariable
    ////------get方式Url传参
    @GetMapping({"/id/{the_Param}"})
    public String id(@PathVariable("the_Param") Integer id) {
        return "id:" + id;
    }
get方式Url传参
2.get方式Url传参:@RequestParam
    ////------get方式Url传参
    @GetMapping(/user)
    public String username(@RequestParam("username") String str) {
        return "username is:" + str;
    }
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;
    }
默认参数

二:配置文件传值使用

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;
    }
配置文件字段使用
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();
        }
    }
获取配置文件组成员

相关文章

  • SpringBoot-02-之参数传递

    一:url传参 1.get方式Url传参:@PathVariable 访问:http://localhost:80...

  • 函数之传递参数

    ECMAScript�中所有的函数都是按值传递的。也就是说把函数外部的值赋值给函数内部的参数,就和把值从一个变量赋...

  • MyBatis传入多个参数 ,List集合

    一.单个参数: 二、多参数: 三、Map封装多参数: 四、List封装in: 五、多参数传递之注解方式示: 六、s...

  • Go语言学习笔记(三)

    指针 Go的指针不能运算 参数传递 Go的参数传递是值传递(值传递:开辟新的空间,拷贝传递参数的值,引用传递:引用...

  • python3 几个知识点

    参数类型 用必选参数、 一定要传递的参数: def shit(a) a就一定要传递 默认参数 可以不传递的参数,有...

  • Python 进阶内容整理

    Python 之禅 参数传递是值传递还是引用传递 深拷贝与浅拷贝 垃圾回收机制 del 元类 (metaclass...

  • Kettle12:Kettle作业和参数

    本章讲解的作业和参数有如下几个案例01 作业02 参数03 表输入参数传递-常量传递04 表输入参数传递-变量传递...

  • 浅谈Java参数传递机制、浅复制(shadow copy)、深复

    1、参数传递机制 参数传递机制分为引...

  • golang笔记之函数

    函数参数传递类型 1.按值传递:Go 默认使用按值传递来传递参数,也就是传递参数的副本。 2.引用传递:如果你希望...

  • 按值传递与按引用传递

    按值传递 -- 传递的是参数的值,即参数本身。 按引用传递 -- 传递的是指向参数的值的引用(指针),而不是参数的...

网友评论

    本文标题:SpringBoot-02-之参数传递

    本文链接:https://www.haomeiwen.com/subject/ycevpftx.html