当前位置:网站首页>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 ;
边栏推荐
- Hands on in-depth learning notes (XIV) 3.7 Simple implementation of softmax regression
- PHP converts a one-dimensional array into a two-dimensional array
- New features of C 10
- 智慧之道(知行合一)
- 面试之 top k问题
- 爱可可AI前沿推介(7.3)
- What material is 13crmo4-5 equivalent to in China? 13crmo4-5 chemical composition 13crmo4-5 mechanical properties
- 什么是质押池,如何进行质押呢?
- CC2530 common registers for crystal oscillator settings
- Arduino esp32: overall framework of lvgl project (I)
猜你喜欢
Daily code 300 lines learning notes day 10
Network security web penetration technology
斑馬識別成狗,AI犯錯的原因被斯坦福找到了
C语言按行修改文件
The word backspace key cannot delete the selected text, so you can only press Delete
Build your own website (23)
建立自己的网站(23)
What is the difference between 14Cr1MoR container plate and 14Cr1MoR (H)? Chemical composition and performance analysis of 14Cr1MoR
What is the material of 13mnnimor? 13mnnimor steel plate for medium and low temperature pressure vessels
UCORE overview
随机推荐
Overview of satellite navigation system
[combinatorics] recursive equation (outline of recursive equation content | definition of recursive equation | example description of recursive equation | Fibonacci Series)
[2. Basics of Delphi grammar] 2 Object Pascal data type
Mysql 单表字段重复数据取最新一条sql语句
Svn usage specification
utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library
手把手带你入门 API 开发
【剑指 Offer 】57 - II. 和为s的连续正数序列
Hands on in-depth learning notes (XIV) 3.7 Simple implementation of softmax regression
On Lagrange interpolation and its application
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
Summary of three methods of PHP looping through arrays list (), each (), and while
How to allow remote connection to MySQL server on Linux system?
PHP secondary domain name session sharing scheme
JSON 与 BSON 区别
To resist 7-Zip, list "three sins"? Netizen: "is the third key?"
数据分析必备的能力
Daily code 300 lines learning notes day 10
Acwing game 58
于文文、胡夏等明星带你玩转派对 皮皮APP点燃你的夏日