Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 多映射的Spring Boot@GetMapping规则_Java_Spring Boot_Request_Get Mapping - Fatal编程技术网

Java 多映射的Spring Boot@GetMapping规则

Java 多映射的Spring Boot@GetMapping规则,java,spring-boot,request,get-mapping,Java,Spring Boot,Request,Get Mapping,我在控制器中有3种不同的方法来获取请求 -第一个使用path变量通过id获取用户: @GetMapping(path="/{id}") public ResponseEntity<UserInfoDTO> getUserById(@PathVariable Long id) 最后是另一个让所有用户 public ResponseEntity<List<UserInfoDTO>> getAllUsers() public ResponseEntity get

我在控制器中有3种不同的方法来获取请求

-第一个使用path变量通过id获取用户:

@GetMapping(path="/{id}")
public ResponseEntity<UserInfoDTO> getUserById(@PathVariable Long id)
最后是另一个让所有用户

public ResponseEntity<List<UserInfoDTO>> getAllUsers()
public ResponseEntity getAllUsers()
第二种和第三种方法的
@GetMapping
应该是什么

例如,所有用户的
@GetMapping
和按用户名的用户的
@GetMapping(path=“/”)

或者别的什么


谢谢。

例如,可选的
用户名
参数:

    @GetMapping(path = "/")
    public ResponseEntity<?> getUserByUsername(@RequestParam(required = false) final String username) {
        if (username != null) {
            // http://localhost:8080/?username=myname
            return new ResponseEntity<>(new UserInfoDTO("by username: " + username), HttpStatus.OK);
        } else {
            // http://localhost:8080/
            return getAllUsers();
        }
    }

    private ResponseEntity<List<UserInfoDTO>> getAllUsers() {
        return new ResponseEntity<>(List.of(new UserInfoDTO("user1-of-all"), new UserInfoDTO("user2-of-all")),
            HttpStatus.OK);
    }

    public static class UserInfoDTO {
        public UserInfoDTO(final String name) {
            this.name = name;
        }

        private final String name;

        public String getName() {
            return name;
        }
    }
@GetMapping(path=“/”)
public ResponseEntity getUserByUsername(@RequestParam(required=false)最终字符串用户名){
如果(用户名!=null){
// http://localhost:8080/?username=myname
返回新的ResponseEntity(newuserinfodto(“按用户名:“+username”),HttpStatus.OK);
}否则{
// http://localhost:8080/
返回getAllUsers();
}
}
私人回应getAllUsers(){
返回新的ResponseEntity(List.of(new UserInfoDTO(“user1 of all”)、new UserInfoDTO(“user2 of all”),
HttpStatus.OK);
}
公共静态类UserInfoDTO{
public UserInfoDTO(最终字符串名){
this.name=名称;
}
私有最终字符串名;
公共字符串getName(){
返回名称;
}
}

例如,可选的
用户名
参数:

    @GetMapping(path = "/")
    public ResponseEntity<?> getUserByUsername(@RequestParam(required = false) final String username) {
        if (username != null) {
            // http://localhost:8080/?username=myname
            return new ResponseEntity<>(new UserInfoDTO("by username: " + username), HttpStatus.OK);
        } else {
            // http://localhost:8080/
            return getAllUsers();
        }
    }

    private ResponseEntity<List<UserInfoDTO>> getAllUsers() {
        return new ResponseEntity<>(List.of(new UserInfoDTO("user1-of-all"), new UserInfoDTO("user2-of-all")),
            HttpStatus.OK);
    }

    public static class UserInfoDTO {
        public UserInfoDTO(final String name) {
            this.name = name;
        }

        private final String name;

        public String getName() {
            return name;
        }
    }
@GetMapping(path=“/”)
public ResponseEntity getUserByUsername(@RequestParam(required=false)最终字符串用户名){
如果(用户名!=null){
// http://localhost:8080/?username=myname
返回新的ResponseEntity(newuserinfodto(“按用户名:“+username”),HttpStatus.OK);
}否则{
// http://localhost:8080/
返回getAllUsers();
}
}
私人回应getAllUsers(){
返回新的ResponseEntity(List.of(new UserInfoDTO(“user1 of all”)、new UserInfoDTO(“user2 of all”),
HttpStatus.OK);
}
公共静态类UserInfoDTO{
public UserInfoDTO(最终字符串名){
this.name=名称;
}
私有最终字符串名;
公共字符串getName(){
返回名称;
}
}

定义映射完全取决于应用程序的上下文及其用例

我们可以定义一个以用户为前缀的上下文,修改后的映射显示在下面的代码段中,在调用时可以像注释中提到的那样调用它

@GetMapping(path="/users/")
public ResponseEntity<UserInfoDTO> getUserByUsername(@RequestParam String username) {
}
// GET: <protocol>://<hostUrl>/users?username=<username>

@GetMapping(path="/users")
public ResponseEntity<List<UserInfoDTO>> getAllUsers() {
}
// GET: <protocol>://<hostUrl>/users

@GetMapping(path="/users/{id}")
public ResponseEntity<UserInfoDTO> getUserById(@PathVariable Long id)
// GET: <protocol>://<hostUrl>/users/<userid>
@GetMapping(path=“/users/”)
公共响应性getUserByUsername(@RequestParam字符串用户名){
}
//获取::///用户?用户名=
@GetMapping(路径=“/users”)
公众反应{
}
//获取::///用户
@GetMapping(path=“/users/{id}”)
公共响应属性getUserById(@PathVariable Long id)
//获取::///用户/

定义映射完全取决于应用程序的上下文及其用例

我们可以定义一个以用户为前缀的上下文,修改后的映射显示在下面的代码段中,在调用时可以像注释中提到的那样调用它

@GetMapping(path="/users/")
public ResponseEntity<UserInfoDTO> getUserByUsername(@RequestParam String username) {
}
// GET: <protocol>://<hostUrl>/users?username=<username>

@GetMapping(path="/users")
public ResponseEntity<List<UserInfoDTO>> getAllUsers() {
}
// GET: <protocol>://<hostUrl>/users

@GetMapping(path="/users/{id}")
public ResponseEntity<UserInfoDTO> getUserById(@PathVariable Long id)
// GET: <protocol>://<hostUrl>/users/<userid>
@GetMapping(path=“/users/”)
公共响应性getUserByUsername(@RequestParam字符串用户名){
}
//获取::///用户?用户名=
@GetMapping(路径=“/users”)
公众反应{
}
//获取::///用户
@GetMapping(path=“/users/{id}”)
公共响应属性getUserById(@PathVariable Long id)
//获取::///用户/

Hello Manta,你是在寻找端点模式还是想为这三种方法创建一个GET端点?嗨,我在这些案例中寻找正确的模式Shello Manta,你是在寻找端点模式还是想为这三种方法创建一个GET端点?嗨,我在这些案例中寻找正确的模式,如果我理解的话,我所做的似乎是正确的模式(因为我在控制器级别定义了
@RequestMapping”/users“
)。谢谢,如果我理解的话,我所做的似乎是正确的模式(因为我在控制器级别定义了一个
@RequestMapping”/users“
)。谢谢