一:url传参
1.get方式Url传参:@PathVariable
@GetMapping({
"/id/{the_Param}"})
public String
id(@
PathVariable("the_Param") Integer id) {
return "id:" + id;
}
访问:http://localhost:8080/id/1994
2.get方式Url传参:@RequestParam
@GetMapping(/user)
public String
username(@
RequestParam("username") String str) {
return "username is:" + str;
}
访问:http://localhost:8080/user?username=toly
3.get方式Url传参:@RequestParam+默认参数
@GetMapping(
"/user_default")
public String
usernameWithDefaultParam(
@
RequestParam(
value =
"username", defaultValue =
"toly1994")
String str) {
return "username is:" + str;
}
访问:http://localhost:8080/user_default
二:配置文件传值使用
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;
}
访问:http://localhost:8080/YoGiOh
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
@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();
}
}
访问:http://localhost:8080/monster
转载于:https://www.cnblogs.com/toly-top/p/9781993.html