欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

@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];
    }
  }

相关标签: Java

上一篇: react路由

下一篇: