自动由接口形成文档 sagger2 使用

SpingBoot 集成 Swagger2 - 简书 (jianshu.com)

sagger2的使用方式看别人的帖子,说的很详细,我就不重复造轮子了

 

配置好后在浏览器访问指定页面就能看到整个后端提供的接口

- import java. util. List; 
http://localhost:8088/swagger-ui . html# 
- @Configu ration 
- @Enab1eSwagge r2 
public class Swagger2Configuration {

 

这个工具的显示效果很好,能展示整个springboot项目中所有controller

页面上展示的controller类都可以被展开,以查看单个controller类下所有带请求注解的方法

能看到方法的请求类型、方法名称

sales-order-controller sales order controller 
[sales/ consi gnee/{consignee} consigneeone 
/sa1es/createReturnOrder create 
/sales/criteria salesorderList 
[sales/ goods/ {orderNumber} goodsList 
/sales/order/ {orderNumber} salesorderone 
[sales/ return/ {orderNumber} returnone 
[sales/ returnGoods returnGoodsList 
/sales/test test 
/sales/updateorder/ {orderNumber} update

 

这些方法还能被进一步点开查看详情

详情中给出了方法的参数表、返回值、状态码,以及示例

/sales/test test 
Parameters 
No parameters 
Responses 
Code 
200 
20000 
Description 
Example Value 
"stri ng" 
Model

 

每个方法展开后都能看到一长串完整的状态码,这是因为在配置swagger时,存入了预设好的状态码

20000 
爨詹詹豺 
22222 
″万髥 不疋4 
22223 
譽趲嫮不疋* 
22224 
22225 
″万繄養虧 
22331 
-t詹寥Z不疋*

 

public enum EnumStatus { 
OK(20000, " ) , 
public Docket createRestApi() { 
ERROR(40000, 
NO LOGIN(33333, 
ACCOUNT ERROR(22222, 
CODE ERROR(22223, 
PASSWOED ERROR(22224, 
NO ACTIVE(22225, 
FORM VALICATOR ERROR(3 
NO UPLOAD(2223344, 
"iLi!H"), 
new ArrayList<>(); 
List<ResponseMessage> responseMessageList 
Arrays . stream(EnumStatus . vaLues()) .forEach(enumStatus -> { 
responsemessage List . add( 
new ResponseMessageBui1der() . code(enumStatus . getStatus( 
new ModelRef(enumStatus . getMessage())) . build()

 

还能在页面实现postman的功能——向后端接口发送请求,比如请求下载文件

/sales/test test 
Parameters 
No parameters 
Responses 
Curl 
Try it out 
Response content type

 

/sales/test test 
Parameters 
No parameters 
Responses 
Cancel 
Execute 
Response content type

 

Execute 
Responses 
Clear 
Response content type 
Curl 
curl 
—X GET "http://IocaIhost:8888/saIes/test" —H "accept: 
Request URL 
http:/AocaIhost:8888/saIes/test 
Server response 
Code 
200 
Details 
Response body 
Download file 
Response headers 
connection: keep—alive 
content—di sposition: form—data • 
content—length: 4152 
content—type: application/octet 
date: Fri, el Oct 2821 
fi ten xls " 
—stream 
GMT

posted @ 2021-10-05 00:18  夏·舍  阅读(80)  评论(0编辑  收藏  举报