当需要快速搭一个内部图书管理系统时,用 Spring Boot + jQuery 做一个前后端分离的架子,比传统的 MVC 轻量不少。这里把接口设计、前端联调的过程记录一下,也聊聊 GET/POST 那些看似基础实则容易出问题的点。
先说数据模型。实体类 BookInfo 就是对照业务字段定义一个 POJO,没什么特别,但字段名要和前端约定好,免得对不上。
@Data
public class BookInfo {
// 图书 ID
private Integer id;
// 书名
private String bookName;
// 作者
private String author;
// 数量
private Integer count;
// 定价
private BigDecimal price;
// 出版社
private String publish;
// 状态 0-不允许借阅 1-允许借阅
private Integer status;
private String statusCN;
// 创建时间
private Date createTime;
// 更新时间
private Date updateTime;
}
登录直接用 UserController 处理,通过 Session 维持状态。实际项目里更多会用 Token,但这个小案例够用了。
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/user")
@RestController
public class UserController {
@RequestMapping("login")
public boolean login(String name, String password, HttpSession session) {
(!StringUtils.hasLength(name) || !StringUtils.hasLength(password)) {
;
}
(.equals(name) && .equals(password)) {
session.setAttribute(, name);
;
}
;
}
}


