当前位置:网站首页>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 ;
边栏推荐
- 深入理解 SQL 中的 Grouping Sets 语句
- Necessary ability of data analysis
- 斑馬識別成狗,AI犯錯的原因被斯坦福找到了
- RF Analyze Demo搭建 Step by Step
- C语言字符串练习
- What kind of material is 14Cr1MoR? Analysis of chemical composition and mechanical properties of 14Cr1MoR
- 于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日
- 數據分析必備的能力
- What material is sa537cl2? Analysis of mechanical properties of American standard container plate
- Visual SLAM algorithms: a survey from 2010 to 2016
猜你喜欢

New features of C 10

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

CC2530 common registers for ADC single channel conversion
The way of wisdom (unity of knowledge and action)

Web crawler knowledge day03

Kotlin学习快速入门(7)——扩展的妙用

ANOVA example

消息队列消息丢失和消息重复发送的处理策略

线程池:业务代码最常用也最容易犯错的组件

What is the maximum number of concurrent TCP connections for a server? 65535?
随机推荐
(Supplement) double pointer topic
Alibaba P8 painstakingly sorted it out. Summary of APP UI automated testing ideas. Check it out
香港理工大学|数据高效的强化学习和网络流量动态的自适应最优周界控制
Golang anonymous function use
How programming apes grow rapidly
CC2530 common registers
深入理解 SQL 中的 Grouping Sets 语句
PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
CC2530 common registers for watchdog
LeetCode 1656. Design ordered flow
网络安全web渗透技术
[Jianzhi offer] 64 Find 1+2+... +n
一台服务器最大并发 tcp 连接数多少?65535?
汇编实例解析--实模式下屏幕显示
Deep understanding of grouping sets statements in SQL
Kotlin learning quick start (7) -- wonderful use of expansion
Why is WPA3 security of enterprise business so important?
Arduino esp32: overall framework of lvgl project (I)
Fast Ethernet and Gigabit Ethernet: what's the difference?
Execute script unrecognized \r


















