http-response

HTTP

异常

1
2
3
4
5
HTTP/1.1 400 Bad Request
{
"code": "ip_illegality",
"message": "你的IP已经被封禁"
}

成功

1
2
3
4
5
{
"username": "cj",
"password": "hash code",
"email": "weicongju@gmail.com"
}

后端

在异常类上使用注解@BusinessException,过滤器检查到该注解后响应异常。过滤器自动把驼峰命名转换成Snake Case作为code。

1
2
3
@BusinessException
class IpIllegalityException extends Exception {
}

@BusinessException("ip")将会把code修改成ip

1
2
3
4
5
HTTP/1.1 400 Bad Request
{
"code": "ip_banned",
"message": "你的IP已经被封禁"
}

前端

1
2
3
4
5
6
7
8
try {
axios.post("/user", {"username": "cj", "password", "123456", "email": "i@imcj.me"})
} catch (e) {
console.debug(e.code)
console.debug(e.message)

this.setData({"message": e.mssage})
}