当前位置:网站首页>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去测一下;
……………………………………………………
接下来的工作就是:用户完善自己的信息后,点击【提交信息】按钮,去提交信息了;;这也是后面将要开发的内容;
边栏推荐
- What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR
- AcWing 第58 场周赛
- CC2530 common registers for ADC single channel conversion
- arduino-esp32:LVGL项目(一)整体框架
- 浅谈拉格朗日插值及其应用
- (补)双指针专题
- How to allow remote connection to MySQL server on Linux system?
- ucore概述
- PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
- Mysql 将逗号隔开的属性字段数据由列转行
猜你喜欢

网络安全web渗透技术

Mysql database -dql

关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM

Simulink oscilloscope data is imported into Matlab and drawn

What material is 12cr1movr? Chemical property analysis of pressure vessel steel plate 12cr1movr

为抵制 7-Zip,列出 “三宗罪” ?网友:“第3个才是重点吧?”

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

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

Data driving of appium framework for mobile terminal automated testing

MySQL Basics
随机推荐
IL Runtime
PHP secondary domain name session sharing scheme
Kotlin learning quick start (7) -- wonderful use of expansion
What is the material of sa302grc? American standard container plate sa302grc chemical composition
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
CC2530 common registers for serial communication
Web crawler knowledge day03
MySQL user management
什么是质押池,如何进行质押呢?
Fast Ethernet and Gigabit Ethernet: what's the difference?
【剑指 Offer】58 - II. 左旋转字符串
JSON 与 BSON 区别
[combinatorics] non descending path problem (number of non descending paths with constraints)
深入理解 SQL 中的 Grouping Sets 语句
Data driving of appium framework for mobile terminal automated testing
汇编实例解析--实模式下屏幕显示
Preventing/catching “IllegalArgumentException: parameter must be a descendant of this view” error
静态程序分析(一)—— 大纲思维导图与内容介绍
[combinatorics] recursive equation (constant coefficient linear homogeneous recursive equation | constant coefficient, linear, homogeneous concept description | constant coefficient linear homogeneous
How to judge the region of an IP through C?


















