当前位置:网站首页>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 ;
边栏推荐
- PHP secondary domain name session sharing scheme
- JSON 与 BSON 区别
- Atom QT 16_ audiorecorder
- Preventing/catching “IllegalArgumentException: parameter must be a descendant of this view” error
- 斑马识别成狗,AI犯错的原因被斯坦福找到了
- 13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
- QT serial port UI design and solution to display Chinese garbled code
- 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
- word 退格键删除不了选中文本,只能按delete
- Execute script unrecognized \r
猜你喜欢
Thread pool executes scheduled tasks
Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..
Meituan side: why does thread crash not cause JVM crash
Fast Ethernet and Gigabit Ethernet: what's the difference?
CC2530 common registers for crystal oscillator settings
CC2530 common registers for serial communication
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Data driving of appium framework for mobile terminal automated testing
What material is sa537cl1? Sa537cl1 corresponds to the national standard material
utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library
随机推荐
Execute script unrecognized \r
Register in PHP_ Globals parameter settings
(Supplement) double pointer topic
ANOVA example
消息队列消息丢失和消息重复发送的处理策略
斑馬識別成狗,AI犯錯的原因被斯坦福找到了
[Jianzhi offer] 58 - ii Rotate string left
What material is sa537cl2? Analysis of mechanical properties of American standard container plate
13mnnimo5-4 German standard steel plate 13MnNiMo54 boiler steel 13MnNiMo54 chemical properties
How to judge the region of an IP through C?
线程池:业务代码最常用也最容易犯错的组件
远程办公之如何推进跨部门项目协作 | 社区征文
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
Idea configuration plug-in
PHP production website active push (website)
Shentong express expects an annual loss of nearly 1billion
How to allow remote connection to MySQL server on Linux system?
汇编实例解析--实模式下屏幕显示
Golang decorator mode and its use in NSQ
What material is sa537cl1? Sa537cl1 corresponds to the national standard material