当前位置:网站首页>29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da
29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da
2022-07-03 16:55:00 【Small withered forest】
explain :
(1) The content of this blog : Development 【 Get user account information , Interface 】;
Catalog
One : Development 【 Get user account information , Interface 】;
(1) natural ,UserController Class to implement UserControllerApi Interface ;
zero : Rationality of this blog ;( Or rather, :【 Get user account information , Interface 】 What is it? )
……………………………………………………
Although I don't understand one for the time being , But the feeling has touched ing The point of truth :
One : Development 【 Get user account information , Interface 】;
1. stay 【api】 Interface engineering , establish UserControllerApi, Definition 【 Get user account information , Interface 】;
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 = " User information related Controller",tags = {" User information related Controller"}) @RequestMapping("user") // Set the routing , This needs to be agreed between the front and back ; public interface UserControllerApi { /** * 【 Get user account information , Interface 】 * @param userId: user id; * @return */ @ApiOperation(value = " Get user account information ", notes = " Get user account information ", httpMethod = "POST") @PostMapping("/getAccountInfo") // Set the routing , This needs to be agreed between the front and back ; public GraceJSONResult getAccountInfo(@RequestParam("userId") String userId); }explain :
(1) Interface url, Request mode , Parameters, etc. , It's all agreed before and after ;( Normally , In the actual project , The team leader An interface document will be provided ;; As the back-end, we , Strictly follow the interface document development )
2. stay 【user】 In the user microservice , establish UserController class , To achieve 【 Get user account information , Interface 】;
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; /** * 【 Get user account information , Interface 】 * @param userId : user id; * @return */ @Override public GraceJSONResult getAccountInfo(String userId) { //0. The judgment parameter cannot be empty ; If it is empty , Just throw a 【 Please log in and continue 】 It's abnormal ; if (StringUtils.isBlank(userId)) { return GraceJSONResult.errorCustom(ResponseStatusEnum.UN_LOGIN); } //1. according to userId, To query user information ; AppUser user = getUser(userId); //2. hold user Medium 【user and userAccountInfoVO, Common attribute , Property value 】copy To userAccountInfoVO; UserAccountInfoVO userAccountInfoVO = new UserAccountInfoVO(); BeanUtils.copyProperties(user,userAccountInfoVO); //3. Return user information ; return GraceJSONResult.ok(userAccountInfoVO); } /** * Common method : according to userId Check it out user; * @param userId * @return */ private AppUser getUser(String userId) { // TODO This method will be used in many places later , And then it will be expanded according to other businesses AppUser user = userService.getUser(userId); return user; } }explain :
(1) natural ,UserController Class to implement UserControllerApi Interface ;
(2) First , Judge some input userId Is it empty ( Used 【StringUtils.isBlank()】 Tool class ), If it is empty, we throw an exception ;;; natural , The specific information of this exception can be defined by yourself ;
( insert ) We are UserService in , Defined a 【 according to userId, Query the user 】 Methods ; then , stay UserServiceImpl This method is realized ;
then , We are UserController in , Inject UserService object ;
(3) because 【 according to userId Query the user 】, There will be many places to use later ;; therefore , We simply made it a public method ;
(4.1) According to the data requirements of the front end , We created UserAccountInfoVO Entity class , Go to the package and find the data : To meet the requirements of the interface for returned data ;( This has many purposes : For example, safety. , Low coupling and so on )
● This idea , I have met before N Time , For example 【Spring Boot E-commerce projects 30: Commodity classification module 9 : The front desk 【 List of categories ( recursive )】 Interface ;(【 establish CategoryVO This bean, Go to the package and find the data : To meet the requirements of the interface for the returned recursive data 】,【 The logic of recursive query 】)】 It was introduced in ;
● stay 【model】 In model engineering , establish vo package , And create UserAccountInfoVO class ;
(4.2) utilize 【BeanUtils.copyProperties(obj1, obj2)】 hold , We 【 according to userId From the database , Checked up AppUser Property value in object ,copy To UserAccountInfoVO Go to the object 】;
● 【BeanUtils.copyProperties(obj1, obj2)】 I have used it before ;
(5) Return information ;
Two : effect ;
(1) First, the whole situation install Look at the whole project ;
(2) then , start-up 【user】 Main startup class of microservice ;
(3) then , have access to Swagger Go and test it ;
……………………………………………………
The next job is : After users improve their information , Click on 【 Submission of information 】 Button , To submit information ;; This is also the content to be developed later ;
边栏推荐
- CC2530 common registers for serial communication
- How to allow remote connection to MySQL server on Linux system?
- Bcvp developer community 2022 exclusive peripheral first bullet
- [Jianzhi offer] 57 - ii Continuous positive sequence with sum s
- 关于视觉SLAM的最先进技术的调查-A survey of state-of-the-art on visual SLAM
- Visual SLAM algorithms: a survey from 2010 to 2016
- 2022爱分析· 国央企数字化厂商全景报告
- Summary of three methods of PHP looping through arrays list (), each (), and while
- PHP secondary domain name session sharing scheme
- 匯編實例解析--實模式下屏幕顯示
猜你喜欢

IDEA-配置插件

arduino-esp32:LVGL项目(一)整体框架

Web crawler knowledge day03

Analysis of variance summary

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

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

What is the maximum number of concurrent TCP connections for a server? 65535?

Processing strategy of message queue message loss and repeated message sending

New features of C 10

消息队列消息丢失和消息重复发送的处理策略
随机推荐
[2. Basics of Delphi grammar] 1 Identifiers and reserved words
PHP CI (CodeIgniter) log level setting
Kotlin学习快速入门(7)——扩展的妙用
The way of wisdom (unity of knowledge and action)
RF analyze demo build step by step
Pytorch 1.12 was released, officially supporting Apple M1 chip GPU acceleration and repairing many bugs
Prepare for the golden three silver four, 100+ software test interview questions (function / interface / Automation) interview questions. win victory the moment one raises one 's standard
How to set up SVN server on this machine
Visual SLAM algorithms: a survey from 2010 to 2016
静态程序分析(一)—— 大纲思维导图与内容介绍
Atom QT 16_ audiorecorder
Necessary ability of data analysis
远程办公之如何推进跨部门项目协作 | 社区征文
Mysql 单表字段重复数据取最新一条sql语句
Analysis of variance summary
C语言字符串反转
Learn from me about the enterprise flutter project: simplified framework demo reference
What is the material of sa302grc? American standard container plate sa302grc chemical composition
Meituan side: why does thread crash not cause JVM crash
MySQL single table field duplicate data takes the latest SQL statement


















