【Uni-App+SSM 宠物项目实战】Day7:SpringMVC 接口测试

72 阅读7分钟

一、前言

前面我们完成了后端 MP 实体类生成(Day6)和前端 Uni-App 项目初始化(Day4),今天终于要打通 “前后端数据交互的关键一步”——用 SpringMVC 开发 API 接口

SpringMVC 是 SSM 框架中的 “请求处理中枢”,就像餐厅里的 “服务员”:前端(Uni-App)发送数据请求(比如 “要一份宠物列表”),SpringMVC 接收请求后,调用后端服务(Service)从数据库取数据,最后把数据打包成 JSON 格式返回给前端。

今天我们重点开发 “宠物信息列表接口”,还会用到项目自定义的 @IgnoreAuth 注解(跳过登录验证),让接口测试更简单。即使你没接触过 SpringMVC 也不用慌,我们从 “创建 Controller” 开始,每一步都带注释,就像搭积木一样逐步完成。

二、今日教学目标

  1. 理解 SpringMVC Controller 的作用(处理前端请求),能独立创建 “宠物列表接口”;
  1. 掌握 @IgnoreAuth 注解的使用场景(公开接口跳过登录),避免 “认证失败” 报错;
  1. 学会用 Postman 测试 API 接口,能识别并解决 “404”“空列表” 等常见问题。

三、前置准备

  1. 已完成 Day6 内容:成功生成 ChongwuxinxiEntity(宠物实体类)和 ChongwuxinxiMapper(MP 接口);
  1. 安装 Postman:官网 Postman 下载免费版,用于发送 HTTP 请求测试接口;
  1. 了解基础概念:
    • Controller:SpringMVC 的核心组件,负责接收 URL 请求(如 /chongwuxinxi/list);
    • Service:业务逻辑层,Controller 会调用 Service 处理数据(如查询数据库);
    • JSON:前后端数据交互的格式,接口返回的 “宠物列表” 会以 JSON 形式传输。

四、代码实现(核心:接口开发全流程)

1. 第一步:创建宠物信息 Controller(处理请求)

Controller 是接口的 “入口”,我们需要创建 ChongwuxinxiController,定义 “宠物列表” 接口的 URL 和请求方式(GET)。

(1)新建 Controller 文件

路径:src/main/java/com/controller/ChongwuxinxiController.java(手动创建 controller 包,若已存在则直接使用)。

(2)编写 Controller 代码

package com.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import com.annotation.IgnoreAuth; // 导入项目自定义的“忽略认证”注解
import com.entity.ChongwuxinxiEntity; // 导入 Day6 生成的宠物实体类
import com.service.ChongwuxinxiService; // 导入宠物服务层接口
/**
 * 宠物信息 Controller:处理宠物相关的 HTTP 请求
 */
@RestController // 关键注解:标记这是“返回 JSON 数据的控制器”(而非页面)
@RequestMapping("/chongwuxinxi") // 基础 URL 路径:所有该 Controller 的接口都以 /chongwuxinxi 开头
public class ChongwuxinxiController {
    // 依赖注入:自动引入宠物服务层的实现类(Spring 帮我们创建实例,不用手动 new)
    @Autowired
    private ChongwuxinxiService chongwuxinxiService;
    /**
     * 宠物列表接口:查询所有宠物信息(公开接口,无需登录)
     * @IgnoreAuth:自定义注解,跳过项目的登录验证(新手必加,否则会报“认证失败”)
     * @GetMapping("/list"):指定请求方式为 GET,完整 URL 是 /chongwuxinxi/list
     */
    @IgnoreAuth
    @GetMapping("/list")
    public List<ChongwuxinxiEntity> getChongwuxinxiList() {
        // 调用 Service 层方法,查询所有宠物(底层用 MP 的 selectList(null))
        return chongwuxinxiService.selectAllChongwuxinxi();
    }
}

(3)补充 @IgnoreAuth 注解定义(若项目未自带)

如果项目中没有 com.annotation.IgnoreAuth,需手动创建注解文件(路径:src/main/java/com/annotation/IgnoreAuth.java),代码如下:

package com.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
// 注解作用范围:只能用在“方法”上(如 Controller 的 list 方法)
@Target(ElementType.METHOD)
// 注解生命周期:运行时生效(Spring 会在运行时识别该注解)
@Retention(RetentionPolicy.RUNTIME)
public @interface IgnoreAuth {
    // 空注解:仅作为“标记”,告诉项目的权限拦截器“跳过该方法的登录验证”
}

2. 第二步:实现 Service 层(处理业务逻辑)

Controller 不直接操作数据库,而是调用 Service 层,Service 层再通过 MP 的 Mapper 查询数据。我们需要创建 ChongwuxinxiService 接口和实现类。

(1)创建 Service 接口

路径:src/main/java/com/service/ChongwuxinxiService.java

package com.service;
import java.util.List;
import com.entity.ChongwuxinxiEntity;
/**
 * 宠物服务层接口:定义宠物相关的业务方法
 */
public interface ChongwuxinxiService {
    // 查询所有宠物信息的方法
    List<ChongwuxinxiEntity> selectAllChongwuxinxi();
}

(2)创建 Service 实现类

路径:src/main/java/com/service/impl/ChongwuxinxiServiceImpl.java

package com.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import com.entity.ChongwuxinxiEntity;
import com.dao.ChongwuxinxiMapper; // 导入 Day6 生成的 MP Mapper
import com.service.ChongwuxinxiService;
/**
 * 宠物服务层实现类:实现 Service 接口的方法
 */
@Service // 关键注解:标记这是“Spring 服务层组件”,让 Controller 能注入
public class ChongwuxinxiServiceImpl implements ChongwuxinxiService {
    // 依赖注入:引入 MP 的 Mapper(Day6 生成,自带 CRUD 方法)
    @Autowired
    private ChongwuxinxiMapper chongwuxinxiMapper;
    /**
     * 实现“查询所有宠物”方法:直接调用 MP Mapper 的 selectList 方法
     */
    @Override
    public List<ChongwuxinxiEntity> selectAllChongwuxinxi() {
        // MP 的 selectList(null):查询所有数据(参数为 null 表示无过滤条件)
        return chongwuxinxiMapper.selectList(null);
    }
}

五、效果验证(用 Postman 测试接口)

1. 启动后端项目

  1. 打开 IDEA,确保项目无编译错误(左侧目录无红色波浪线);
  1. 点击顶部 “启动” 按钮(绿色三角),等待项目启动成功(控制台显示 Tomcat started on port(s): 8080 (http))。

2. 用 Postman 发送请求

(1)打开 Postman 并新建请求

  1. 打开 Postman,点击左上角 File → New → Request
  1. 输入请求名称(如 “宠物列表接口测试”),选择请求方式为 GET,点击 Save

(2)输入接口 URL 并发送

  1. 在 URL 输入框中输入:http://localhost:8080/chongwuxinxi/list(注意端口与项目一致,默认 8080);
  1. 点击右侧 Send 按钮,发送 GET 请求。

(3)查看返回结果(预期效果)

  • 若数据库 chongwuxinxi 表有数据(如之前插入的 “小狗”),Postman 会返回 JSON 格式的列表:
[    {        "id": 1,        "mingcheng": "小狗",        "pinzhong": "柯基",        "nianling": "2岁",        "createTime": "2024-05-20 10:30:00"    }]
  • 若表中无数据,会返回空列表 [](需先插入测试数据,参考 Day5 的 SQL 插入方法)。

六、常见问题与解决方案

问题描述可能原因解决方案
发送请求后,Postman 报 “404 Not Found”1. Controller 的 @RequestMapping 路径写错(如少写 /chongwuxinxi);2. 项目未配置 SpringMVC 拦截器,无法识别 Controller;3. 端口号错误(如项目实际端口是 8081,URL 用了 8080)1. 检查 ChongwuxinxiController 的 @RequestMapping 是否为 /chongwuxinxi,接口 URL 是否为 /chongwuxinxi/list;2. 确认项目 web.xml 中配置了 SpringMVC 的前端控制器(如 DispatcherServlet);3. 查看 IDEA 启动日志,确认 Tomcat 端口(搜索 “Tomcat started on port (s)”)
Postman 报 “401 Unauthorized(认证失败)”未给接口加 @IgnoreAuth 注解,项目权限拦截器要求登录才能访问1. 在 getChongwuxinxiList() 方法上添加 @IgnoreAuth 注解;2. 重启项目后重新测试
返回空列表 [],但数据库有数据1. Service 层未注入 Mapper(chongwuxinxiMapper 为 null);2. 实体类字段与数据库表字段不匹配(MP 查不到数据)1. 检查 ChongwuxinxiServiceImpl 是否加了 @Service 注解,chongwuxinxiMapper 是否用 @Autowired 注入;2. 确认 ChongwuxinxiEntity 的字段名与数据库表 chongwuxinxi 的字段名一致(如数据库字段 mingcheng,实体类字段也为 mingcheng)
IDEA 启动时报 “Could not autowire. No beans of 'ChongwuxinxiService' type found”Service 实现类未加 @Service 注解,Spring 无法扫描到该组件在 ChongwuxinxiServiceImpl 类上添加 @Service 注解,重启项目

七、工具类 / 框架特性拓展

1. @IgnoreAuth 注解的使用场景

@IgnoreAuth 是项目权限体系的 “例外开关”,仅适用于 公开接口,比如:

  • 首页推荐的宠物列表、商家列表;
  • 登录页、注册页的接口(还没登录,肯定要跳过验证)。

绝对不能用在需要登录的接口,比如:

  • 用户个人的宠物档案修改接口;
  • 订单提交、购物车结算接口(防止未登录用户操作)。

2. SpringMVC 的核心优势

相比传统的 Servlet(需要手动处理请求参数、转换 JSON),SpringMVC 有两大便利:

  1. 自动参数绑定:如果接口需要传参(如 getById(Long id)),SpringMVC 会自动把 URL 中的 id 参数绑定到方法参数上,不用手动解析;
  1. 自动 JSON 转换:加了 @RestController 注解后,SpringMVC 会自动把 List 转换成 JSON 格式返回,不用手动调用 JSON.toJSONString()。

八、结语

今天我们成功开发并测试了第一个 SpringMVC 接口 —— 你的 Postman 能正常拿到宠物列表数据吗?

如果遇到 “404 路径错误”“认证失败” 或 “返回空列表”,欢迎在评论区分享你的:

  • 报错截图或错误日志(关键部分即可);

我们一起分析问题原因,确保大家都能打通 “后端接口” 这一关!

下期预告

Day8:Uni-App 网络请求(前端调用今天开发的 /chongwuxinxi/list 接口,在 “宠物” Tab 页展示宠物列表,实现真正的 “前后端数据交互”)。