当前位置:网站首页>29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
2022-07-03 16:53:00 【小枯林】
说明:
(1)本篇博客内容:开发【获得用户账户信息,接口】;
目录
零:本篇博客合理性说明;(或者说是:【获得用户账户信息,接口】是什么)
1.在【api】接口工程中,创建UserControllerApi,定义【获得用户账户信息,接口】;
2.在【user】用户微服务中,创建UserController类,去实现【获得用户账户信息,接口】;
(1)自然,UserController类要实现UserControllerApi接口;
(2)首先,判断一些入参userId是否为空(使用了【StringUtils.isBlank()】工具类),如果为空我们就抛一个异常;;;自然,这个异常的具体信息可以自己定义;
(插)我们在UserService中,定义了一个【根据userId,查询用户】的方法;然后,在UserServiceImpl实现了该方法;
(3)因为【根据userId查询用户】,在后面将会有很多地方用到;;所以,我们干脆抽成了一个公用方法;
(4.1)根据前端对数据的需求,我们创建UserAccountInfoVO实体类,去包装查到的数据:以符合接口对返回数据的要求;(这样做有很多目的:比如安全,耦合性低等等)
零:本篇博客合理性说明;(或者说是:【获得用户账户信息,接口】是什么)
……………………………………………………
一个虽然暂时不明白,但感觉已经触及ing真相的点:
一:开发【获得用户账户信息,接口】;
1.在【api】接口工程中,创建UserControllerApi,定义【获得用户账户信息,接口】;
UserControllerApi:
package com.imooc.api.controller.user; import com.imooc.grace.result.GraceJSONResult; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import javax.servlet.http.HttpServletRequest; @Api(value = "用户信息相关Controller",tags = {"用户信息相关Controller"}) @RequestMapping("user") //设置路由,这个是需要前后端约定好的; public interface UserControllerApi { /** * 【获得用户账户信息,接口】 * @param userId:用户id; * @return */ @ApiOperation(value = "获得用户账户信息", notes = "获得用户账户信息", httpMethod = "POST") @PostMapping("/getAccountInfo") //设置路由,这个是需要前后端约定好的; public GraceJSONResult getAccountInfo(@RequestParam("userId") String userId); }说明:
(1)接口的url,请求方式,参数等,都是前后端约定好的;(正常来说,在实际项目中,团队leader会提供一份接口文档;;作为后端的我们来说,严格按照接口文档开发即可)
2.在【user】用户微服务中,创建UserController类,去实现【获得用户账户信息,接口】;
UserController:
package com.imooc.user.controller; import com.imooc.api.BaseController; import com.imooc.api.controller.user.PassportControllerApi; import com.imooc.api.controller.user.UserControllerApi; import com.imooc.bo.RegistLoginBo; import com.imooc.enums.UserStatus; import com.imooc.grace.result.GraceJSONResult; import com.imooc.grace.result.ResponseStatusEnum; import com.imooc.pojo.AppUser; import com.imooc.user.service.UserService; import com.imooc.utils.IPUtil; import com.imooc.utils.SMSUtils; import com.imooc.vo.UserAccountInfoVO; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.validation.BindingResult; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.util.Map; import java.util.UUID; @RestController public class UserController implements UserControllerApi { final static Logger logger = LoggerFactory.getLogger(UserController.class); @Autowired private UserService userService; /** * 【获得用户账户信息,接口】 * @param userId :用户id; * @return */ @Override public GraceJSONResult getAccountInfo(String userId) { //0.判断参数不能为空;如果为空,就抛出一个【请登录后再继续操作】的异常; if (StringUtils.isBlank(userId)) { return GraceJSONResult.errorCustom(ResponseStatusEnum.UN_LOGIN); } //1.根据userId,去查询用户信息; AppUser user = getUser(userId); //2.把user中的【user和userAccountInfoVO,共有的属性的,属性值】copy到userAccountInfoVO; UserAccountInfoVO userAccountInfoVO = new UserAccountInfoVO(); BeanUtils.copyProperties(user,userAccountInfoVO); //3.返回用户信息; return GraceJSONResult.ok(userAccountInfoVO); } /** * 公用方法:根据userId去查user; * @param userId * @return */ private AppUser getUser(String userId) { // TODO 本方法后续会被很多地方公用,而且后续还要根据其他业务进行扩展 AppUser user = userService.getUser(userId); return user; } }说明:
(1)自然,UserController类要实现UserControllerApi接口;
(2)首先,判断一些入参userId是否为空(使用了【StringUtils.isBlank()】工具类),如果为空我们就抛一个异常;;;自然,这个异常的具体信息可以自己定义;
(插)我们在UserService中,定义了一个【根据userId,查询用户】的方法;然后,在UserServiceImpl实现了该方法;
然后,我们在UserController中,注入UserService对象;
(3)因为【根据userId查询用户】,在后面将会有很多地方用到;;所以,我们干脆抽成了一个公用方法;
(4.1)根据前端对数据的需求,我们创建UserAccountInfoVO实体类,去包装查到的数据:以符合接口对返回数据的要求;(这样做有很多目的:比如安全,耦合性低等等)
● 这种思想,自己以前遇到过N次,比如在【Spring Boot电商项目30:商品分类模块九:前台的【分类列表(递归)】接口;(【创建CategoryVO这个bean,去包装查到的数据:以符合接口对返回的递归数据的要求】,【递归查询的逻辑】)】中就介绍过;
● 在【model】模型工程中,创建vo包,并创建UserAccountInfoVO类;
(4.2)利用【BeanUtils.copyProperties(obj1, obj2)】把,我们【根据userId从数据库中,查到的AppUser对象中的属性值,copy到UserAccountInfoVO对象中去】;
● 【BeanUtils.copyProperties(obj1, obj2)】自己以前也使用过;
(5)返回信息;
二:效果;
(1)先全局install一下整个项目;
(2)然后,启动【user】微服务的主启动类;
(3)然后,可以使用Swagger去测一下;
……………………………………………………
接下来的工作就是:用户完善自己的信息后,点击【提交信息】按钮,去提交信息了;;这也是后面将要开发的内容;
边栏推荐
- 深入理解 SQL 中的 Grouping Sets 语句
- There are several APIs of airtest and poco that are easy to use wrong in "super". See if you have encountered them
- Build your own website (23)
- 智慧之道(知行合一)
- IDEA-配置插件
- MySQL converts comma separated attribute field data from column to row
- 关于学习Qt编程的好书精品推荐
- To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
- JSON 与 BSON 区别
- Threejs Part 2: vertex concept, geometry structure
猜你喜欢

Data driving of appium framework for mobile terminal automated testing

Simulink oscilloscope data is imported into Matlab and drawn

于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日

Shentong express expects an annual loss of nearly 1billion

爱可可AI前沿推介(7.3)

Mysql 单表字段重复数据取最新一条sql语句

Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer

Static program analysis (I) -- Outline mind map and content introduction

What is the pledge pool and how to pledge?

Daily code 300 lines learning notes day 10
随机推荐
How to allow remote connection to MySQL server on Linux system?
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Why is WPA3 security of enterprise business so important?
CC2530 common registers
一台服务器最大并发 tcp 连接数多少?65535?
[2. Basics of Delphi grammar] 1 Identifiers and reserved words
What is the pledge pool and how to pledge?
Web crawler knowledge day03
Network security web penetration technology
Add color to the interface automation test framework and realize the enterprise wechat test report
NLP四范式:范式一:非神经网络时代的完全监督学习(特征工程);范式二:基于神经网络的完全监督学习(架构工程);范式三:预训练,精调范式(目标工程);范式四:预训练,提示,预测范式(Prompt工程)
[2. Basics of Delphi grammar] 2 Object Pascal data type
【剑指 Offer 】64. 求1+2+…+n
【剑指 Offer】58 - II. 左旋转字符串
[combinatorics] polynomial theorem (polynomial coefficients | full arrangement of multiple sets | number of schemes corresponding to the ball sub model | polynomial coefficient correlation identity)
【剑指 Offer】58 - I. 翻转单词顺序
ucore概述
2022爱分析· 国央企数字化厂商全景报告
UCORE overview
C语言按行修改文件


















