1.springbootå建apiï¼
springbootå建apiï¼
源码aide源码导入截图ææ¯è¿æ ·ä½¿ç¨SpringBoot(APIä¼ å)
springbootä¸çControlleræè RestControlleræ¥æ¶åæ°çæ¹æ³æ¯ä¸æ ·çãè¿ç« ç®æ æ¯å¯¹å ç§å¸¸ç¨çä¼ åé½å个ä¾åã
源码aide源码导入截图å建package:com.biboheart.demos.apiï¼è¿ä¸ªå éæ¾ç½®APIæ¥å£çRestController
源码aide源码导入截图å¨com.biboheart.demos.apiå ä¸å建ä¸ä¸ªclass:ParamControllerãè¿ç« ç示ä¾é½å¨è¿ä¸ªç±»ä¸è¿è¡ã
源码aide源码导入截图è·¯å¾ä¸å å«åæ°ï¼è¿ç§æ åµéåä¸ä¼ éä¸ä¸ªä¸è½ä¸ºç©ºå¼çåæ°ãæ¯å¦æäºåºç¨ä¸ï¼æ¥è¯¢æ个ä¼ä¸çæ°æ®ï¼å¿ é¡»å å«ä¼ä¸çç¼å·ï¼å°±å¯ä»¥å¨å°åéæ¥æ¶è¿ä¸ªç¼å·ã
源码aide源码导入截图å¨ParamControllerä¸å¢å ä¸ä¸ªå½æ°PathParam
源码aide源码导入截图è·¯å¾ä¸çåæ°éè¿{ sn}æ¥æ¶å¼ã
源码aide源码导入截图使ç¨å称æ¥æ¶åæ°æ¯è¾ç´è§ï¼æç¨å¾æ¯è¾å¤ãä¸ç¨æ³¨è§£ãè·å®¢æ·ç«¯ä¼ çåæ°ååå°±å¯ä»¥æ¥æ¶å°ãå¦ä¸ä¾åä¸çreqParam
源码aide源码导入截图ä¹å¯ä»¥ç¨getä¼ å
源码aide源码导入截图ä¸é¢ç¨åæ°å称æ¥æ¶å¼ï¼åªè¦ä¸è¯·æ±æ¶çå称ä¸è´å°±è½æ¥æ¶å°åæ°å¼ãå¦æéè¦ä¼ éçåæ°æ¯è¾å¤çæ¶åï¼å°±ä¸éåä¸ä¸ªä¸ä¸ªåæ°åå¨å½æ°éäºï¼é£æ ·å¾ä¸æ¹ä¾¿ï¼ä¹å®¹æåºéãå¯ä»¥æè¿äºåæ°åå°ä¸ä¸ªå¯¹è±¡éæ¥æ¶ãå¦ä¸ä¾å
源码aide源码导入截图å建ä¸ä¸ªpackage:com.biboheart.demos.modelï¼å建ä¸ä¸ªç±»Person
源码aide源码导入截图ç¨Person对象æ¥æ¶åæ°
源码aide源码导入截图è½ç¶è¿æ ·çæ¹å¼ä¹è½æ¥æ¶getä¼ åï¼ä½æ¯å 为éè¦ä¼ çåæ°å¤äºï¼ç¨getæ¾å¾ä¸å¤ªæ¹ä¾¿ãè¿éå°±ä¸æµè¯GETæ¹å¼è¯·æ±äºã
源码aide源码导入截图æçæ¶åï¼éè¦æ¥æ¶JSONä¼ åãæ¯å¦æäºæå¡çè°ç¨éè¦å¼åä¸ä¸ªåè°å½æ°æ¥æ¶å¯¹æ¹çåè°å¹¶è·å¾åæ°å¼ä¸ºç»æï¼å¯¹æ¹åè°åæ¥çå¼æ¯JSONæ ¼å¼çå¼ï¼è¿æ¶åå°±éè¦æ¥æ¶JSONä¼ åæ°äºãç¨RequestBodyå¯ä»¥æ¥æ¶å°JSONä¼ å¼ã
源码aide源码导入截图éè¦JSON请æ±æè½æ£ç¡®æ¥å°å¼
源码aide源码导入截图SpringBootå®ä¾:å»é¢ç»ä¸ä¿¡æ¯å¹³å°(apigateway)
源码aide源码导入截图åé¢å·²ç»å¨å¹³å°ä¸ä½¿ç¨äºspringcloudãæ¯ä¸ªå°çæå¡ä¸åèªå®ç°ç¸å ³ä¸å¡ï¼æä¾APIãè¿äºæå¡ç访é®å°åé½å¯è½ä¸ä¸æ ·ãè¿æ ·ç»ä½¿ç¨é½é æå°æ°ï¼èä¸æå¡å¨æ¥å£ç®¡çä¹å¤æäºã
源码aide源码导入截图api-gatewayå°±æ¯æè¿äºapiéè¿ä¸ä¸ªæå¡æä¾åºå»ãå¨è¿ä¸ªæå¡ä¸ä»£çå ¶å®æå¡çAPIã对äºæå¡ç使ç¨é½å°±åæ¯è®¿é®ä¸å°æå¡å¨ã
源码aide源码导入截图è¿éç¨springzuulå®ç°api-gatewayã
源码aide源码导入截图å建ä¸ä¸ªé¡¹ç®ï¼æå¡ï¼ï¼ä¸é¨åapi代çãæå¡å称huip-routerã
源码aide源码导入截图pom
源码aide源码导入截图RouterApplication
源码aide源码导入截图为äºæ¯æè·¨åï¼å¢å ä¸ä¸ªFilter
源码aide源码导入截图é ç½®
源码aide源码导入截图å¨API请æ±å¼å¤´ä¸º/huipuser/æ¶è®¿é®çæ¯useræå¡çAPIï¼å¦æå¼å¤´/huippatient/æ¶è®¿é®çæ¯patientæå¡çAPIãæ¯å¦è¯·æ±ç¸å½äº
源码aide源码导入截图请æ±æµè¯ã
源码aide源码导入截图SpringBootå®ä¾:å»é¢ç»ä¸ä¿¡æ¯å¹³å°(æå¡é´é讯)ç访é®æµç¨æ¹æç¨ä»£çã
源码aide源码导入截图åºäºSpringBootçAPIæµè¯äºèç½äº§åçæµè¯çç¥ç°å¨å¾å¤é½ä¼åå¨APIæµè¯ãè½»é级GUIæµè¯ãè½»é级åå æµè¯çãAPIæµè¯å ¶å®æ们ä¸å¼å§æ³å¾æå¤çå¾å½¢åå·¥å ·åºè¯¥æ¯postmanãjmeterçãå¦æ使ç¨æç®åçgetæ¹æ³ï¼è¿å¯ä»¥ç´æ¥éè¿ä½¿ç¨CURLå½ä»¤ï¼å³å½ä»¤è¡å·¥å ·cURLï¼ã
源码aide源码导入截图ä¸ç®¡ä½¿ç¨ä»ä¹APIæµè¯å·¥å ·ï¼APIæµè¯çåºæ¬æ¥éª¤å¤§ä½ä¸è´ï¼
源码aide源码导入截图1.åå¤æµè¯æ°æ®
源码aide源码导入截图2.éè¿APIæµè¯å·¥å ·ï¼å起对被æµAPIçrequest
源码aide源码导入截图3.éªè¯è¿åç»æçresponse
源码aide源码导入截图æ们平æ¶å¨å·¥ä½ä¸ï¼æ¥è§¦å¾æå¤çæ¯ç¨JAVAæ¡æ¶Springbootæ¡æ¶å¼åçç®åçRestfulAPIã
源码aide源码导入截图Springboot建议çç®å½ç»æå¦ä¸ï¼rootpackageç»æ-com.example.myproject
源码aide源码导入截图ç§ç§äººå®¶ç¨SpringBootåçå端APIæ¥å£ï¼é£å«ä¸ä¸ªä¼éå设å®ç°ä¸ä¸ªæ³¨åç¨æ·çåè½ï¼å¨controllerå±ï¼ä»ä¼å è¿è¡æ ¡éªåæ°ï¼å¦ä¸ï¼
源码aide源码导入截图以ä¸ä»£ç æä»ä¹é®é¢åï¼å ¶å®æ²¡ä»ä¹é®é¢ï¼å°±æ¯æ ¡éªæç¹è¾£ç¼çãæ£å¸¸çæ·»å ç¨æ·ä¸å¡è¿æ²¡åï¼åæ°æ ¡éªå°±ä¸å¤§å å¦ãå设åæ¥ï¼åæ¥äºä¸ä¸ªéæ±ï¼ç¼è¾ç¨æ·ä¿¡æ¯ãå®ç°ç¼è¾ç¨æ·ä¿¡æ¯åï¼ä¹æ¯å æ ¡éªä¿¡æ¯ï¼å¦ä¸ï¼
源码aide源码导入截图æ们å¯ä»¥ä½¿ç¨æ³¨è§£çæ¹å¼ï¼æ¥è¿è¡åæ°æ ¡éªï¼è¿æ ·ä»£ç æ´å ç®æ´ï¼ä¹æ¹ä¾¿ç»ä¸ç®¡çãå®é ä¸ï¼springbootæ个validationçç»ä»¶ï¼æ们å¯ä»¥æ¿æ¥å³ç¨ãå¼å ¥è¿ä¸ªå å³å¯ï¼
源码aide源码导入截图å¼å ¥å åï¼åæ°æ ¡éªå°±é常ç®æ´å¦ï¼å¦ä¸ï¼
源码aide源码导入截图ç¶åå¨UserParamåæ°å¯¹è±¡ä¸ï¼å å ¥@Validated注解åï¼æé误信æ¯æ¥æ¶å°BindingResult对象ï¼ä»£ç å¦ä¸ï¼
源码aide源码导入截图å¦æä½ å¨ä½ 们项ç®ä»£ç ä¸ï¼çå°controllerå±æ¥æè¿åç»æï¼æè¿æ ·çï¼
源码aide源码导入截图ä¹æè¿æ ·çï¼
源码aide源码导入截图æ¾ç¶ï¼å¦ææ¥å£è¿åç»æä¸ç»ä¸ï¼å端å¤çå°±ä¸æ¹ä¾¿ï¼æ们代ç ä¹ä¸å¥½ç»´æ¤ãåæ¯å¦æç人å欢ç¨Resultå¤çç»æï¼æç¹äººå欢ç¨Responseå¤çç»æï¼å¯ä»¥æ³è±¡ä¸ä¸ï¼è¿äºä»£ç æå¤ä¹±ã
源码aide源码导入截图æ以ä½ä¸ºå端å¼åï¼æ们项ç®çååºç»æï¼éè¦ç»ä¸æ åçè¿åæ ¼å¼ãä¸è¬ä¸ä¸ªæ åçååºæ¥æ对象ï¼é½æåªäºå±æ§å¢ï¼
源码aide源码导入截图ååºç¶æç ä¸è¬ç¨æ举表示åï¼
源码aide源码导入截图å 为è¿åçæ°æ®ç±»åä¸æ¯ç¡®å®çï¼æ们å¯ä»¥ä½¿ç¨æ³åï¼å¦ä¸ï¼
源码aide源码导入截图æäºç»ä¸çååºä½ï¼æ们就å¯ä»¥ä¼åä¸ä¸controllerå±ç代ç å¦ï¼
源码aide源码导入截图æ¥å¸¸å¼åä¸ï¼æ们ä¸è¬é½æ¯èªå®ä¹ç»ä¸çå¼å¸¸ç±»ï¼å¦ä¸ï¼
源码aide源码导入截图å¨controllerå±ï¼å¾å¯è½ä¼æ类似代ç ï¼
源码aide源码导入截图è¿å代ç ï¼æ²¡ä»ä¹é®é¢åï¼ä½æ¯å¦ætry...catch太å¤ï¼ä¸æ¯å¾ä¼é ã
源码aide源码导入截图å¯ä»¥åå©æ³¨è§£@RestControllerAdviceï¼è®©ä»£ç æ´ä¼é ã@RestControllerAdviceæ¯ä¸ä¸ªåºç¨äºControllerå±çåé¢æ³¨è§£ï¼å®ä¸è¬é å@ExceptionHandler注解ä¸èµ·ä½¿ç¨ï¼ä½ä¸ºé¡¹ç®çå ¨å±å¼å¸¸å¤çãæ们æ¥çä¸demo代ç åã
源码aide源码导入截图è¿æ¯åæ¥çUserControllerï¼åä¸ä¸ªä¼æåºå¼å¸¸çuserServiceçæ¹æ³ï¼å¦ä¸ï¼
源码aide源码导入截图æ们åå®ä¹ä¸ä¸ªå ¨å±å¼å¸¸å¤çå¨ï¼ç¨@RestControllerAdvice注解ï¼å¦ä¸ï¼
源码aide源码导入截图æ们ææ³è¦æ¦æªçå¼å¸¸ç±»åï¼æ¯å¦æ³æ¦æªBizExceptionç±»åï¼å°±æ°å¢ä¸ä¸ªæ¹æ³ï¼ä½¿ç¨@ExceptionHandler注解修饰ï¼å¦ä¸ï¼
源码aide源码导入截图SpringBoot2åºäºSwagger2çæ离线Apiææ¡£Github:
源码aide源码导入截图Gitee:
源码aide源码导入截图个人è§å¾æ§ççé ç½®ç®å许å¤ï¼æ°ççé ç½®æç §å®æ¹demoçé ç½®æ¥åè¿æ¯å¤æäºå¾å¤
源码aide源码导入截图é ç½®å°Springboot项ç®ä¸ä»¥åï¼å¨é¡¹ç®æå çæ¶å便ä¼éè¿åå æµè¯å¨æå®çç®å½çæ被å®æ¹ç§°ä¸ºstaticdocsç离线ææ¡£
源码aide源码导入截图该ç¯åæå¼ç¨çä¾èµé½è¦å¼å ¥ï¼SpringRestDocsçä¾èµspring-restdocs-mockmvcï¼ç¦»çº¿ææ¡£çä¾èµspringfox-staticdocsï¼å 为è¦å¨åå æµè¯çæ¶åçæææ¡£ï¼æ以éè¦åå æµè¯ç¸å ³çspring-boot-starter-testã
源码aide源码导入截图asciidoctor-maven-pluginæ件ä¼æAsciidocæ ¼å¼æ件转æHTML5æ ¼å¼è¾åºã
源码aide源码导入截图è¿ä¸ªç±»å å«ä¸¤ä¸ªæ¹æ³ï¼TestApi()æ¯ç¨æ¥çæä¾åï¼test()ç¨æ¥çæAsciidocçææ¡£ãçæä¾åç¨å°äºspring-restdocs-mockmvcï¼æ¯ä¸ä¸ªAPIé½è¦è¿è¡åå æµè¯æè½çæç¸åºçææ¡£ç段ï¼snippetsï¼ï¼çæçç»æå¦å¾ï¼
源码aide源码导入截图çæå®æ´çAsciidocææ¡£ç¨å°äºSwagger2MarkupConverterï¼ç¬¬ä¸æ¥å è·åå¨çº¿çæ¬çæ档并ä¿åå°æ件swagger.jsonä¸ï¼ç¬¬äºæ¥æswagger.jsonåä¹åçä¾åsnippetsæ´å并ä¿å为Asciidocæ ¼å¼çå®æ´ææ¡£ãçæç»æå¦å¾ï¼
源码aide源码导入截图éè¿é 置类å®ä¹ä¸äºææ¡£ç¸å ³çä¿¡æ¯
源码aide源码导入截图è·¯å¾ï¼é¡¹ç®å/docs/asciidoc/index.adoc
源码aide源码导入截图å©ç¨åé¢é ç½®çmavenæ件ï¼åªéè¦æ§è¡æå å°±å¯ä»¥çæç¸åºçææ¡£ï¼å¦å¾ï¼
源码aide源码导入截图该ç¯åæå¼ç¨çä¾èµé½è¦å¼å ¥ï¼SpringRestDocsçä¾èµspring-restdocs-mockmvcï¼ç¦»çº¿ææ¡£çä¾èµspringfox-staticdocsï¼å 为è¦å¨åå æµè¯çæ¶åçæææ¡£ï¼æ以éè¦åå æµè¯ç¸å ³çspring-boot-starter-testã
源码aide源码导入截图asciidoctor-maven-pluginæ件ä¼æAsciidocæ ¼å¼æ件转æHTML5æ ¼å¼è¾åºã
源码aide源码导入截图è¿ä¸ªç±»å å«ä¸¤ä¸ªæ¹æ³ï¼TestApi()æ¯ç¨æ¥çæä¾åï¼createSpringfoxSwaggerJson()ç¨æ¥çæAsciidocçææ¡£ãçæä¾åç¨å°äºspring-restdocs-mockmvcï¼æ¯ä¸ä¸ªAPIé½è¦è¿è¡åå æµè¯æè½çæç¸åºçææ¡£ç段ï¼snippetsï¼ï¼çæçç»æå¦å¾ï¼
源码aide源码导入截图çæå®æ´çAsciidocææ¡£ç¨å°äºSwagger2MarkupConverterï¼ç¬¬ä¸æ¥å è·åå¨çº¿çæ¬çæ档并ä¿åå°æ件swagger.jsonä¸ï¼ç¬¬äºæ¥æswagger.jsonåä¹åçä¾åsnippetsæ´å并ä¿å为Asciidocæ ¼å¼çå®æ´ææ¡£ãçæç»æå¦å¾ï¼
源码aide源码导入截图éè¿é 置类å®ä¹ä¸äºææ¡£ç¸å ³çä¿¡æ¯
源码aide源码导入截图å¨resourcesç®å½ä¸å建ä¸ä¸ªå为logback.xmlçé ç½®æ件ï¼ä½¿ç¨LogstashEncoderä½ä¸ºDefaultLogEncoder
源码aide源码导入截图è·¯å¾ï¼é¡¹ç®åsrc/docs/asciidoc/index.adoc
源码aide源码导入截图å©ç¨åé¢é ç½®çmavenæ件ï¼åªéè¦æ§è¡æå å°±å¯ä»¥çæç¸åºçææ¡£ï¼å¦å¾ï¼
源码aide源码导入截图源码aide源码导入截图