Java后端基础练习|请求参数
package com.urfread.breaknews.core.controller;
import com.urfread.breaknews.core.common.model.ResultData;
import lombok.Data;
import org.springframework.web.bind.annotation.*;
/**
* Class Description: A brief description of the functionality and purpose of this class.
*
* @author urfread
* @date 2024-10-08 15:08
*/
@RestController
@RequestMapping("/practice")
public class PracticeController {
// 路径参数
@GetMapping("/path/{username}")
public ResultData<String> welcomeWithPathVariable(@PathVariable String username) {
return ResultData.success(username);
}
// 查询参数
@GetMapping("/query")
public ResultData<User> welcomeWithQueryParameter(User user) {
return ResultData.success(user);
}
// 请求体
@PostMapping("/body")
public ResultData<User> welcomeWithRequestBody(@RequestBody User user) {
return ResultData.success(user);
}
// 请求头
@GetMapping("/header")
public ResultData<String> welcomeWithHeader(@RequestHeader("username") String username) {
return ResultData.success(username);
}
}
@Data
class User {
private String username;
private String password;
private String email;
}