202209280848

2023-11-27  本文已影响0人  hemiao3000

示例

@Data  
@NoArgsConstructor  
@AllArgsConstructor  
@Schema(description = "用户登录信息")  
public class LoginToken {  
  
    @Schema(name = "username", example = "tommy", required = true, description = "用户名")  
    private String username;  
  
    @Schema(name = "password", example = "123456", required = true, description = "登录密码")  
    private String password;  
}

示例一:GET + query-string(写法一)

这种写法<mark style="background: #FFB8EBA6;">没有用</mark>到“收参数”的 Java Bean<small>(即,FO)</small>,参数是“散着”传进来的。

@Operation(summary = "GET 请求 1", description = "GET + query-string", tags = {"用户管理"}, method="GET" )
@GetMapping(value = "/get-query-string-1", 
//          consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, // GET 请求没有请求体,所以 GET 请求的请求头中没有 content-type
            produces = MediaType.APPLICATION_JSON_VALUE             // 本 URI 返回什么格式的数据
)
public ResponseResult<String> login1(
        @Parameter(in = ParameterIn.QUERY, description = "用户名", required = true) 
        @RequestParam(value = "username") String username,
        @Parameter(in = ParameterIn.QUERY, description = "密码", required = true) 
        @RequestParam(value = "password") String password) {
    …
}    

示例二:GET + query-string(写法二)

@Operation( summary = "GET 请求 2", description = "GET + query-string", method="GET" )  
@GetMapping(path = "/get-query-string-2", 
            produces = MediaType.APPLICATION_JSON_VALUE
)  
public ResponseEntity<String> login2(  
        @Parameter(in = ParameterIn.QUERY, description = "登录认证信息", schema = @Schema(implementation = LoginToken.class))  
        LoginToken token
)

示例三:POST + query-string

@Operation(summary = "POST 请求 1", description = "POST + query-string 参数", tags = {"用户管理"}, method="POST")  
@PostMapping(value = "/post-query-string",  
             consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE,  
             produces = MediaType.APPLICATION_JSON_VALUE  
)  
public ResponseEntity<String> worldUsingPostUsingPOST1(  
        @Parameter(in = ParameterIn.DEFAULT, description = "登录认证信息", schema = @Schema(implementation = LoginToken.class))
        LoginToken token  
);

示例四:POST + json-string

@Operation(summary = "POST 请求 2", description = "POST + json-string 参数", tags = {"用户管理"}, method="POST")
@PostMapping(value = "/post-json-string",  
             consumes = MediaType.APPLICATION_JSON_VALUE,
             produces = MediaType.APPLICATION_JSON_VALUE  
)
public ResponseEntity<String> worldUsingPostUsingPOST2(
        @Parameter(in = ParameterIn.DEFAULT,  description = "登录认证信息",  schema = @Schema(implementation = LoginToken.class)) 
        @RequestBody LoginToken token  
) 
上一篇 下一篇

猜你喜欢

热点阅读