具体设计
请求类型 |
URL |
功能说明 |
|
GET |
/USER |
查询列表 |
|
POST |
/USER |
创建 |
|
PUT |
/USER/ID |
根据id修改修改 |
|
GET |
/USER/ID |
根据id获取 |
|
DELETE |
/USER/ID |
根据id删除 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @PathVariable
@RequestBody
-------------------------------------- @RestController @RequestMapping(value = "/users")
@GetMapping("/")
@PostMapping("/")
@GetMapping("/{id}")
@PutMapping("/{id}")
@DeleteMapping("/{id}")
|
公司的响应信息化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
@ToString @NoArgsConstructor @AllArgsConstructor @Accessors(chain = true) @ApiModel(value = "响应信息主体") public class R<T> implements Serializable { private static final long serialVersionUID = 1L;
@Getter @Setter @ApiModelProperty(value = "返回标记:成功标记=0,失败标记=1") private int code;
@Getter @Setter @ApiModelProperty(value = "返回信息") private String msg;
@Getter @Setter @ApiModelProperty(value = "数据") private T data;
public static <T> R<T> ok() { return restResult(null, CommonConstants.SUCCESS, null); }
public static <T> R<T> ok(T data) { return restResult(data, CommonConstants.SUCCESS, null); }
public static <T> R<T> ok(T data, String msg) { return restResult(data, CommonConstants.SUCCESS, msg); }
public static <T> R<T> failed() { return restResult(null, CommonConstants.FAIL, null); }
public static <T> R<T> failed(String msg) { return restResult(null, CommonConstants.FAIL, msg); }
public static <T> R<T> failed(T data) { return restResult(data, CommonConstants.FAIL, null); }
public static <T> R<T> failed(T data, String msg) { return restResult(data, CommonConstants.FAIL, msg); }
private static <T> R<T> restResult(T data, int code, String msg) { R<T> apiResult = new R<>(); apiResult.setCode(code); apiResult.setData(data); apiResult.setMsg(msg); return apiResult; } }
|
一些资源状态码
4xx状态码表示客户端错误,主要有下面几种。
400 Bad Request:服务器不理解客户端的请求,未做任何处理。
401 Unauthorized:用户未提供身份验证凭据,或者没有通过身份验证。
403 Forbidden:用户通过了身份验证,但是不具有访问资源所需的权限。
404 Not Found:所请求的资源不存在,或不可用。
405 Method Not Allowed:用户已经通过身份验证,但是所用的 HTTP 方法不在他的权限之内。
410 Gone:所请求的资源已从这个地址转移,不再可用。
415 Unsupported Media Type:客户端要求的返回格式不支持。比如,API 只能返回 JSON 格式,但是客户端要求返回 XML 格式。
422 Unprocessable Entity :客户端上传的附件无法处理,导致请求失败。
429 Too Many Requests:客户端的请求次数超过限额。
5xx状态码表示服务端错误。一般来说,API 不会向用户透露服务器的详细信息,所以只要两个状态码就够了。
500 Internal Server Error:客户端请求有效,服务器处理时发生了意外。
503 Service Unavailable:服务器无法处理请求,一般用于网站维护状态。