Skip to content

🌴基于SpringBoot的WebApi示例项目(Swagger/sql2o-plus)

Notifications You must be signed in to change notification settings

cotide/moni-webapi

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

moni-webapi

Build Status

基于SpringBoot的WebApi示例

访问地址

http://localhost:9200/swagger-ui.html

引用

访问 pom.xml

特点

1.统一结果返回(fastJson),格式如下:

属性 描述
code 状态编码
isSuccess 请求是否成功 (指当前业务处理是否正确)
msg 消息
data 数据

1.1 没有结果数据-格式

{
  "code": 200,
  "isSuccess": true, 
  "msg": ""
}

1.2 有结果数据-格式

{
  "code": 200,
  "data": {
    "createTime": "2018-09-05 18:03:15",
    "login": 1000,
    "userId": 29,
    "userName": "Hello World"
  },
  "isSuccess": true,
  "msg": ""
}

1.3 异常数据-格式

状态 描述 JSON
401 非法权限 {"code":401,"isSuccess":false,"msg":"异常信息"}
500 服务器异常 {"code":500,"isSuccess":false,"msg":"异常信息"}
510 请求参数异常 {"code":510,"isSuccess":false,"msg":"异常信息"}
510 业务异常 {"code":511,"isSuccess":false,"msg":"异常信息"}

2.Swagger的集成 (多版本)

Swagger的集成 (多版本)

3.使用sql2o-plus

4.权限控制

权限控制

需要权限访问的接口,请求时候需要带访问Token信息(Token鉴权使用jwt

4.1 @PowerFilter

控制器级别 - 标记该注解的Class需要权限才允许访问

4.1.1 使用

继承AuthApiController

4.2 @ActionPowerFilter

Action级别 - 标记该注解的方法需要权限才允许访问

4.2.1 使用

对需要权限的方法标记@ActionPowerFilter注解

@ActionPowerFilter 
@RequestMapping(value = "get",method = RequestMethod.GET)
public void get(){}

5.自定义格式

5.1 @IgnoreRequestFilter

忽略全局格式,使用自定义格式

@ApiOperation(value = "获取自定义格式")
@IgnoreRequestFilter
@RequestMapping(value = "getMyResult",method = RequestMethod.GET)
public String getMyResult(){}

5.2 @Download

文件下载

@ApiOperation(value = "Excel下载")
@Download
@RequestMapping(value = "downloadExcel",method = RequestMethod.GET)
public void downloadExcel(
        HttpServletRequest request,
        HttpServletResponse response)
        throws IOException,
        IllegalAccessException,
        InstantiationException,
        ClassNotFoundException {}

6 图片二维码合并输出 (缓存)

@Download
@Cacheable(value = "qr_getGithubQR",key = "#userName")
@Scheduled(fixedRate = ONE_DAY )
@ApiOperation(value = "获取Github二维码")
@RequestMapping(
        value = "getGithubQR",
        method = RequestMethod.GET,
        produces = MediaType.IMAGE_PNG_VALUE
)
public byte[] getGithubQR(String userName)
        throws IOException, WriterException {

    BufferedImage in = ImageIO.read(getClass().getResourceAsStream("/static/img/rq_bg.png"));
    if(in==null)
    {
        throw new NullPointerException("未找到背景图");
    }
    BufferedImage qrImg = QRCodeUtil.toBufferedImage("https://github.com/"+userName,240,240);
    BufferedImage outPut = QRCodeUtil.mergeImage(in,qrImg,200,170);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        ImageIO.write(outPut, "png", output);
        output.flush();
        return output.toByteArray();
    }finally {
        output.close();
    }
}

图片二维码合并输出 (缓存)

资源

Releases

No releases published

Packages

No packages published

Languages