当前位置:网站首页>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 ;
边栏推荐
- AcWing 第58 场周赛
- 爱可可AI前沿推介(7.3)
- Interpretation of several important concepts of satellite antenna
- Arduino esp32: overall framework of lvgl project (I)
- Necessary ability of data analysis
- 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
- Thread pool executes scheduled tasks
- [combinatorics] recursive equation (constant coefficient linear homogeneous recursive equation | constant coefficient, linear, homogeneous concept description | constant coefficient linear homogeneous
- 27. 输入3个整数,按从大到小的次序输出。要求用指针方法实现。
- 【剑指 Offer】58 - II. 左旋转字符串
猜你喜欢

手把手带你入门 API 开发

NSQ source code installation and operation process

Processing strategy of message queue message loss and repeated message sending

Why is WPA3 security of enterprise business so important?

CC2530 common registers for crystal oscillator settings

斑馬識別成狗,AI犯錯的原因被斯坦福找到了

A survey of state of the art on visual slam

What is the material of 13mnnimor? 13mnnimor steel plate for medium and low temperature pressure vessels

utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library

Visual SLAM algorithms: a survey from 2010 to 2016
随机推荐
Thread pool executes scheduled tasks
Static program analysis (I) -- Outline mind map and content introduction
[sword finger offer] 58 - I. flip the word order
PHP secondary domain name session sharing scheme
What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
Kotlin学习快速入门(7)——扩展的妙用
function overloading
[JDBC] API parsing
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
NSQ source code installation and operation process
PHP converts a one-dimensional array into a two-dimensional array
Le zèbre a été identifié comme un chien, et la cause de l'erreur d'AI a été trouvée par Stanford
A survey of state of the art on visual slam
Idea configuration plug-in
面试之 top k问题
Summary of three methods of PHP looping through arrays list (), each (), and while
【剑指 Offer】58 - I. 翻转单词顺序
AcWing 第58 场周赛
Web crawler knowledge day03


















