《用户中心》后端项目controller中的deleteUser的测试与修改

《用户中心》中的直播视频中,没有对用户中心的管理方法deleteUser进行测试,此处补充下这部分内容。按视频中deleteUser方法的代码:

===

@PostMapping("/delete")

public boolean deleteUser(@RequestBody long id, HttpServletRequest request){

if(!isAdmin(request)){

return false;

}

if(id <= 0){

return false;

}

boolean res = userService.removeById(id);

return res;

}

===


我们使用如下的request测试:

===

POST http://localhost:8080/user/delete

Content-Type: application/json


{

"id": "1"

}

===


测试不成功,出现如下的错误描述:

Required request parameter ‘id’ for method parameter type long is not present

后端没有拿到id这个参数,而request中的确已经给出这个数据。猜测后端没有那么智能,能将1这个整形数据直接提取出来。那么我们将deleteUser接口重写,书写代码提取其中的id整型数据:

===

@PostMapping("/delete")

public boolean deleteUser(@RequestBody Map<String, String> dataBody, HttpServletRequest request){

if(!isAdmin(request)){

return false;

}

String idstr = dataBody.get("id");

if(idstr==null || idstr.isEmpty()){

return false;

}

long id=-1;

try{

id = Long.parseLong(idstr);

}catch (NumberFormatException e){

return false;

}


if(id <= 0){

return false;

}

boolean res = userService.removeById(id);

return res;

}

===

保存重新启动测试,这次对了,返回的值为true(如下图),再查看数据库id=1的数据的isDelete位的确被置为1,说明逻辑删除正确执行。

0个评论
点击登录,快来和大家讨论吧~
表情
图片
暂无评论
tfzhang
作者分享
后端 学习总结# #直播笔记# 《https://wx.zsxq.com/mweb/views/weread/search.html?keyword=用户中心》后端学习记录2024 之前过了一遍《https://wx.zsxq.com/mweb/views/weread/search.html?keyword=用户中心》的前端开发和部署,现在开始《https://wx.zsxq.com/mweb/views/weread/search.html?keyword=用户中心》后端的开发学习,记录下操作过程,供只想学习后端的鱼油参考。源代码在最后的链接,当前还未完结,不定期更新。 1、后端spring项目初始化与数据库设计 http://66bond.com/yupi/userback/usercenter1.html 2、后端mybatis-plus访问数据库demo测试 http://66bond.com/yupi/userback/usercenter2.html 3、后端实现用户注册 http://66bond.com/yupi/userback/usercenter3.html 4、后端实现用户登录 http://66bond.com/yupi/userback/usercenter4.html 5、后端实现controller层中的登录与注册 http://66bond.com/yupi/userback/usercenter5.html 6、后端实现controller的用户查找与删除接口 http://66bond.com/yupi/userback/usercenter6.html 《https://wx.zsxq.com/mweb/views/weread/search.html?keyword=用户中心》前5章对应的源码: http://66bond.com/yupi/userback/code/backend.zip
16
学习总结 #前端# 《https://wx.zsxq.com/mweb/views/weread/search.html?keyword=用户中心》前端部分学习记录2024 前端部分终于完结,后续后端部分再来一遍。 用户中心1:前端初始化与瘦身记录 http://66bond.com/yupi/usercenter/user-center1.html 用户中心2:前端登录页定制化修改 http://66bond.com/yupi/usercenter/user-center2.html 用户中心3:前端登录页访问后端接口修改 http://66bond.com/yupi/usercenter/user-center3.html 用户中心4:模仿登录页写注册页 http://66bond.com/yupi/usercenter/user-center4.html 用户中心5:登录页添加注册链接及登录后跳转到Welcome页 http://66bond.com/yupi/usercenter/user-center5.html 用户中心6:完成后台用户查询页 http://66bond.com/yupi/usercenter/user-center6.html 用户中心7:前端对后端异常封装后的适配 http://66bond.com/yupi/usercenter/user-center7.html 用户中心8:前端打包和部署 http://66bond.com/yupi/usercenter/user-center8.html 适配前5章及第6章的后端代码: http://66bond.com/yupi/usercenter/code/适配前端前5章的后端代码.zip 适配第7章的后端代码: http://66bond.com/yupi/usercenter/code/codeforchap7.zip
17
前端 学习总结 #直播笔记# 《https://wx.zsxq.com/mweb/views/weread/search.html?keyword=用户中心》前端操作记录2024年 新增第6章和第7章。 现在刷鱼总的《https://wx.zsxq.com/mweb/views/weread/search.html?keyword=用户中心》项目,想着一口气过下前端,只记录前端的相关操作,后端不提及, 后端可以直接使用鱼总的代码或者我提供的适配前端(见下方链接)。 用户中心1:前端初始化与瘦身记录 http://66bond.com/yupi/usercenter/user-center1.html 用户中心2:前端登录页定制化修改 http://66bond.com/yupi/usercenter/user-center2.html 用户中心3:前端登录页访问后端接口修改 http://66bond.com/yupi/usercenter/user-center3.html 用户中心4:模仿登录页写注册页 http://66bond.com/yupi/usercenter/user-center4.html 用户中心5:登录页添加注册链接及登录后跳转到Welcome页 http://66bond.com/yupi/usercenter/user-center5.html 用户中心6:完成后台用户查询页 http://66bond.com/yupi/usercenter/user-center6.html 用户中心7:前端对后端异常封装后的适配 http://66bond.com/yupi/usercenter/user-center7.html 适配前5章及第6章的后端代码: http://66bond.com/yupi/usercenter/code/适配前端前5章的后端代码.zip 适配第7章的后端代码: http://66bond.com/yupi/usercenter/code/codeforchap7.zip
11
前端 #学习总结# 直播笔记 用户中心前端记录2024年 现在刷鱼总的《https://wx.zsxq.com/mweb/views/weread/search.html?keyword=用户中心》项目,想着一口气过下前端,只记录前端的相关操作,后端不提及, 后端可以直接使用鱼总的代码或者我提供的适配前端(前5章)的后端代码(见下方链接)。 用户中心1:前端初始化与瘦身记录 http://66bond.com/yupi/usercenter/user-center1.html 用户中心2:前端登录页定制化修改 http://66bond.com/yupi/usercenter/user-center2.html 用户中心3:前端登录页访问后端接口修改 http://66bond.com/yupi/usercenter/user-center3.html 用户中心4:模仿登录页写注册页 http://66bond.com/yupi/usercenter/user-center4.html 用户中心5:登录页添加注册链接及登录后跳转到Welcome页 http://66bond.com/yupi/usercenter/user-center5.html 未完待续。。。 适配前5章的后端代码: http://66bond.com/yupi/usercenter/code/适配前端前5章的后端代码.zip
14
前端 学习总结 #直播笔记# 用户中心前端学习记录2024年 现在刷鱼总的《https://wx.zsxq.com/mweb/views/weread/search.html?keyword=用户中心》项目,暂时忽略了后端(直接拉鱼总的后端项目),想一口气将前端先过一遍,顺便记录下笔记,供暂时只想学前端的参考。 用户中心1-前端初始化与瘦身记录 http://66bond.com/yupi/usercenter/user-center1.html 用户中心2-前端登陆页面定制化修改 http://66bond.com/yupi/usercenter/user-center2.html 用户中心3-前端登陆页访问后端接口修改 http://66bond.com/yupi/usercenter/user-center3.html 未完待续。。。
14
下载 APP