@PathVariable注解的使用
程序员文章站
2024-02-14 15:01:04
...
一、映射路径变量得到的请求参数:
// http://localhost:8081/api/v1.0/users/ming
@GetMapping("/{name}")
@PathVariable String name
{"createDate":"2021-08-12T15:36:03.801055+08:00","name":"ming"}
// http://localhost:8081/api/v1.0/users/list/11
@GetMapping("list/{ids}")
@PathVariable long[] ids
[{"id":11}]
二、例子:
@RequestMapping("/api/v1.0/users")
public class UserProfileController {
private static CxLogger logger = CxLogManager.getLogger();
@GetMapping("/{name}/{age}")
public UserProfile hello(@PathVariable String name, @PathVariable Integer age) {
UserProfile profile = new UserProfile();
profile.setName(name);
profile.setName(age);
profile.setCreateDate(OffsetDateTime.now());
return profile;
}
@GetMapping("list/{ids}")
public UserProfile[] list(@PathVariable long[] ids) {
if(ids != null ){
UserProfile[] result = new UserProfile[ids.length];
for (int i = 0; i < ids.length; i++) {
UserProfile profile = new UserProfile();
profile.setId(ids[i]);
result[i] = profile;
}
return result;
}
return new UserProfile[0];
}
}
上一篇: react路由