当前位置:网站首页>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去测一下;
……………………………………………………
接下来的工作就是:用户完善自己的信息后,点击【提交信息】按钮,去提交信息了;;这也是后面将要开发的内容;
边栏推荐
- 数据分析必备的能力
- 消息队列消息丢失和消息重复发送的处理策略
- 关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
- 什么是质押池,如何进行质押呢?
- Cocos Creator 2. X automatic packaging (build + compile)
- Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
- Summary of three methods of PHP looping through arrays list (), each (), and while
- The way of wisdom (unity of knowledge and action)
- CC2530 common registers for ADC single channel conversion
- The word backspace key cannot delete the selected text, so you can only press Delete
猜你喜欢

QT serial port UI design and solution to display Chinese garbled code

What is the pledge pool and how to pledge?

What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR

Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs

NSQ source code installation and operation process

手把手带你入门 API 开发

NLP four paradigms: paradigm 1: fully supervised learning in the era of non neural networks (Feature Engineering); Paradigm 2: fully supervised learning based on neural network (Architecture Engineeri

Unreal_ Datatable implements ID self increment and sets rowname

静态程序分析(一)—— 大纲思维导图与内容介绍

Mysql 将逗号隔开的属性字段数据由列转行
随机推荐
27. 输入3个整数,按从大到小的次序输出。要求用指针方法实现。
CC2530 common registers for port initialization
What material is sa537cl2 equivalent to in China? Sa537cl2 corresponding material
斑马识别成狗,AI犯错的原因被斯坦福找到了
C语言字符串反转
Register in PHP_ Globals parameter settings
Central South University | through exploration and understanding: find interpretable features with deep reinforcement learning
Build your own website (23)
JSON 与 BSON 区别
Visual SLAM algorithms: a survey from 2010 to 2016
[Jianzhi offer] 58 - ii Rotate string left
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Mysql 单表字段重复数据取最新一条sql语句
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
线程池执行定时任务
What is the maximum number of concurrent TCP connections for a server? 65535?
Hands on in-depth learning notes (XIV) 3.7 Simple implementation of softmax regression
Visual SLAM algorithms: a survey from 2010 to 2016
What is the pledge pool and how to pledge?
Mysql 将逗号隔开的属性字段数据由列转行


















