当前位置:网站首页>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去测一下;
……………………………………………………
接下来的工作就是:用户完善自己的信息后,点击【提交信息】按钮,去提交信息了;;这也是后面将要开发的内容;
边栏推荐
- JSON 与 BSON 区别
- To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
- 2022.02.14_ Daily question leetcode five hundred and forty
- CC2530 common registers for timer 1
- [combinatorics] recursive equation (constant coefficient linear homogeneous recursive equation | constant coefficient, linear, homogeneous concept description | constant coefficient linear homogeneous
- C语言字符串反转
- [2. Basics of Delphi grammar] 2 Object Pascal data type
- Deep understanding of grouping sets statements in SQL
- IDEA-配置插件
- PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
猜你喜欢
Yu Wenwen, Hu Xia and other stars take you to play with the party. Pipi app ignites your summer
2022爱分析· 国央企数字化厂商全景报告
Network security web penetration technology
A survey of state of the art on visual slam
斑马识别成狗,AI犯错的原因被斯坦福找到了
静态程序分析(一)—— 大纲思维导图与内容介绍
Simulink oscilloscope data is imported into Matlab and drawn
What material is 13crmo4-5 equivalent to in China? 13crmo4-5 chemical composition 13crmo4-5 mechanical properties
Add color to the interface automation test framework and realize the enterprise wechat test report
Web crawler knowledge day03
随机推荐
Mysql 单表字段重复数据取最新一条sql语句
Hong Kong Polytechnic University | data efficient reinforcement learning and adaptive optimal perimeter control of network traffic dynamics
(补)双指针专题
比亚迪、长城混动市场再“聚首”
CC2530 common registers for timer 1
PHP production website active push (website)
Assembly instance analysis -- screen display in real mode
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Acwing game 58
MySQL converts comma separated attribute field data from column to row
A survey of state of the art on visual slam
[combinatorics] polynomial theorem (polynomial coefficients | full arrangement of multiple sets | number of schemes corresponding to the ball sub model | polynomial coefficient correlation identity)
C语言字符串反转
Static program analysis (I) -- Outline mind map and content introduction
On Lagrange interpolation and its application
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
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
CC2530 common registers for serial communication