基于SpringBoot的WebApi示例
访问 pom.xml
属性 | 描述 |
---|---|
code | 状态编码 |
isSuccess | 请求是否成功 (指当前业务处理是否正确) |
msg | 消息 |
data | 数据 |
{
"code": 200,
"isSuccess": true,
"msg": ""
}
{
"code": 200,
"data": {
"createTime": "2018-09-05 18:03:15",
"login": 1000,
"userId": 29,
"userName": "Hello World"
},
"isSuccess": true,
"msg": ""
}
状态 | 描述 | 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":"异常信息"} |
3.使用sql2o-plus
需要权限访问的接口,请求时候需要带访问Token信息(Token鉴权使用jwt)
控制器级别 - 标记该注解的Class需要权限才允许访问
Action级别 - 标记该注解的方法需要权限才允许访问
对需要权限的方法标记@ActionPowerFilter注解
@ActionPowerFilter
@RequestMapping(value = "get",method = RequestMethod.GET)
public void get(){}
忽略全局格式,使用自定义格式
@ApiOperation(value = "获取自定义格式")
@IgnoreRequestFilter
@RequestMapping(value = "getMyResult",method = RequestMethod.GET)
public String getMyResult(){}
文件下载
@ApiOperation(value = "Excel下载")
@Download
@RequestMapping(value = "downloadExcel",method = RequestMethod.GET)
public void downloadExcel(
HttpServletRequest request,
HttpServletResponse response)
throws IOException,
IllegalAccessException,
InstantiationException,
ClassNotFoundException {}
@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();
}
}