116 lines
3.9 KiB
Java
116 lines
3.9 KiB
Java
|
|
package com.example.demo.controller;
|
|||
|
|
|
|||
|
|
import org.springframework.beans.factory.annotation.Value;
|
|||
|
|
import org.springframework.web.bind.annotation.*;
|
|||
|
|
|
|||
|
|
import java.util.HashMap;
|
|||
|
|
import java.util.Map;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 学习示例控制器
|
|||
|
|
*
|
|||
|
|
* 学习点:
|
|||
|
|
* - 各种参数接收方式
|
|||
|
|
* - 配置注入
|
|||
|
|
* - 响应格式
|
|||
|
|
*/
|
|||
|
|
@RestController
|
|||
|
|
public class LearnController {
|
|||
|
|
|
|||
|
|
// 从配置文件注入值
|
|||
|
|
@Value("${spring.application.name:demo}")
|
|||
|
|
private String appName;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 根路径 - 重定向到学习中心
|
|||
|
|
*/
|
|||
|
|
@GetMapping("/")
|
|||
|
|
public Map<String, Object> root() {
|
|||
|
|
Map<String, Object> info = new HashMap<>();
|
|||
|
|
info.put("message", "欢迎来到 Spring Boot 学习脚手架!");
|
|||
|
|
info.put("learn", "https://spring.xiaoxiaoluohao.indevs.in/learn");
|
|||
|
|
info.put("aop", "https://spring.xiaoxiaoluohao.indevs.in/aop");
|
|||
|
|
info.put("api", "https://spring.xiaoxiaoluohao.indevs.in/api/users");
|
|||
|
|
return info;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GET /learn - API 信息
|
|||
|
|
@GetMapping("/learn")
|
|||
|
|
public Map<String, Object> info() {
|
|||
|
|
Map<String, Object> info = new HashMap<>();
|
|||
|
|
info.put("app", appName);
|
|||
|
|
info.put("message", "欢迎学习 Spring Boot!");
|
|||
|
|
info.put("endpoints", new String[]{
|
|||
|
|
"GET /learn/params?name=xxx&age=18 - 参数示例",
|
|||
|
|
"POST /learn/body - JSON 请求体示例",
|
|||
|
|
"GET /learn/path/{id} - 路径变量示例",
|
|||
|
|
"GET /learn/header - 请求头示例",
|
|||
|
|
"GET /learn/cookie - Cookie 示例"
|
|||
|
|
});
|
|||
|
|
return info;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GET /learn/params?name=xxx&age=18 - 查询参数
|
|||
|
|
@GetMapping("/learn/params")
|
|||
|
|
public Map<String, Object> params(
|
|||
|
|
@RequestParam(required = false, defaultValue = "游客") String name,
|
|||
|
|
@RequestParam(required = false, defaultValue = "0") Integer age
|
|||
|
|
) {
|
|||
|
|
Map<String, Object> result = new HashMap<>();
|
|||
|
|
result.put("name", name);
|
|||
|
|
result.put("age", age);
|
|||
|
|
result.put("tip", "使用 @RequestParam 接收查询参数");
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// POST /learn/body - 请求体
|
|||
|
|
@PostMapping("/learn/body")
|
|||
|
|
public Map<String, Object> body(@RequestBody Map<String, Object> data) {
|
|||
|
|
Map<String, Object> result = new HashMap<>();
|
|||
|
|
result.put("received", data);
|
|||
|
|
result.put("tip", "使用 @RequestBody 接收 JSON 请求体");
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GET /learn/path/{id} - 路径变量
|
|||
|
|
@GetMapping("/learn/path/{id}")
|
|||
|
|
public Map<String, Object> path(@PathVariable String id) {
|
|||
|
|
Map<String, Object> result = new HashMap<>();
|
|||
|
|
result.put("id", id);
|
|||
|
|
result.put("tip", "使用 @PathVariable 接收路径变量");
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GET /learn/header - 请求头
|
|||
|
|
@GetMapping("/learn/header")
|
|||
|
|
public Map<String, Object> header(@RequestHeader(value = "User-Agent", required = false) String userAgent) {
|
|||
|
|
Map<String, Object> result = new HashMap<>();
|
|||
|
|
result.put("userAgent", userAgent);
|
|||
|
|
result.put("tip", "使用 @RequestHeader 获取请求头");
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GET /learn/cookie - Cookie
|
|||
|
|
@GetMapping("/learn/cookie")
|
|||
|
|
public Map<String, Object> cookie(@CookieValue(value = "JSESSIONID", required = false) String sessionId) {
|
|||
|
|
Map<String, Object> result = new HashMap<>();
|
|||
|
|
result.put("sessionId", sessionId);
|
|||
|
|
result.put("tip", "使用 @CookieValue 获取 Cookie");
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GET /learn/exception - 异常处理
|
|||
|
|
@GetMapping("/learn/exception")
|
|||
|
|
public String exception() {
|
|||
|
|
throw new RuntimeException("这是一个测试异常");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 全局异常处理
|
|||
|
|
@ExceptionHandler(RuntimeException.class)
|
|||
|
|
public Map<String, Object> handleException(RuntimeException e) {
|
|||
|
|
Map<String, Object> result = new HashMap<>();
|
|||
|
|
result.put("error", e.getMessage());
|
|||
|
|
result.put("tip", "使用 @ExceptionHandler 处理异常");
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
}
|