当前位置:网站首页>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去测一下;
……………………………………………………
接下来的工作就是:用户完善自己的信息后,点击【提交信息】按钮,去提交信息了;;这也是后面将要开发的内容;
边栏推荐
- New features of C 10
- Hands on in-depth learning notes (XIV) 3.7 Simple implementation of softmax regression
- Golang decorator mode and its use in NSQ
- 【剑指 Offer 】64. 求1+2+…+n
- CC2530 common registers for port interrupts
- The word backspace key cannot delete the selected text, so you can only press Delete
- Acwing game 58
- 27. 输入3个整数,按从大到小的次序输出。要求用指针方法实现。
- CC2530 common registers for ADC single channel conversion
- CC2530 common registers for crystal oscillator settings
猜你喜欢

13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties

Mysql 将逗号隔开的属性字段数据由列转行

Netease UI automation test exploration: airtest+poco

什么是质押池,如何进行质押呢?

What is the pledge pool and how to pledge?

MySQL Basics

New features of C 10

NSQ source code installation and operation process

Visual SLAM algorithms: a survey from 2010 to 2016

2022爱分析· 国央企数字化厂商全景报告
随机推荐
【剑指 Offer 】57 - II. 和为s的连续正数序列
C语言字符串练习
C语言按行修改文件
The way of wisdom (unity of knowledge and action)
Hands on in-depth learning notes (XIV) 3.7 Simple implementation of softmax regression
【剑指 Offer】58 - II. 左旋转字符串
How programming apes grow rapidly
Kindeditor editor upload image ultra wide automatic compression -php code
What material is sa537cl2? Analysis of mechanical properties of American standard container plate
How to delete a specific line from a text file using the SED command?
手把手带你入门 API 开发
PHP secondary domain name session sharing scheme
What is the material of sa302grc? American standard container plate sa302grc chemical composition
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
IDEA-配置插件
NSQ source code installation and operation process
arduino-esp32:LVGL项目(一)整体框架
数据分析必备的能力
LeetCode 1658. Minimum operand to reduce x to 0
How to set up SVN server on this machine


















