java~api返回值的标准化

api返回值的标准化

例如

{"status":200,"message":"操作成功","data":"{"id":1,"name":"张三"}"}

封装返回对象

对象被封装在base.util.ResponseUtils类型下,返回值是标准的ResponseEntity对象,返回体

进行了二次封装,主要有status,messsage和data组成,返回方法有ok和okMessage,如果

真是返回消息,不需要对象,可以选择使用okMessage,反之使用ok方法。

封装的返回对象:

 @Builder
 @Getter
 @NoArgsConstructor
 @AllArgsConstructor
 static class ResponseBody {
 private int status;
 private String message;
 private Object data;
 }

httpError和我们封装的httpError

对于http error来说有很多种,基本可以定为code在400到500之间的,像客户端参数问题就是400- bad request,而没有认证就是401-Unauthorized,认证但没有对应的权限就是403-Forbidden,请求的

资源没有发现就是404-Not Found,请求方式错误(方法是post,你发起请求用了get)就是405- Method Not Allowed等。

  • 使用标准http响应状态码
 @GetMapping(GET_HTTP_ERROR)
 ResponseEntity<?> getHttpError() throws IOException {
 return ResponseEntity.badRequest().build();
 }
 @Test
 public void getHttpError() throws Exception {
 mockMvc
 .perform(
 get(LindDemo.GET_HTTP_ERROR)
 .accept(MediaType.APPLICATION_JSON_UTF8))
 .andExpect(status().is(400));
 
 }
 

响应的结果

MockHttpServletResponse:
 Status = 400
 Error message = null
 Headers = {}
 Content type = null
 Body = 
 Forwarded URL = null
 Redirected URL = null
 Cookies = []
  • 使用我们封装的status状态码
 @GetMapping(GET_ERROR)
 ResponseEntity<?> getError() throws IOException {
 return ResponseUtils.badRequest("传入的参数非法!");
 }
 
 @Test
 public void getError() throws Exception {
 mockMvc
 .perform(
 get(LindDemo.GET_ERROR)
 .accept(MediaType.APPLICATION_JSON_UTF8))
 .andExpect(status().isOk());
 
 }

响应的结果

MockHttpServletResponse:
 Status = 200
 Error message = null
 Headers = {Content-Type=[application/json;charset=UTF-8]}
 Content type = application/json;charset=UTF-8
 Body = {"status":400,"message":"传入的参数非法!","data":{}}
 Forwarded URL = null
 Redirected URL = null
 Cookies = []

通过上面的响应结果可以看到,我们封装的请求httpcode还是200,只不过把请求错误400状态码写在了body

对象里,目前这种方法用的比较多,像一些第三方接口用的都是这种方式,他们会规定相应的响应规范。

总结

事实上,两种响应体都没有问题,关键在于开发之间的规则要确定,不要在项目里两者兼用!

这里是程序员秘密聚集地,各位还在架构师的道路上挣扎的小伙伴们速来。“

加QQ群:585550789(名额有限哦!)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值