当前位置:   article > 正文

springboot 定义统一返回结果_resultcode":200,"resultmsg":"success","resultdata

resultcode":200,"resultmsg":"success","resultdata

前后端分离项目中后端开发需要写相应的接口,定义统一的返回格式有利于提高开发效率和沟通的成本。通常返回的格式主要如下两种:
1.    只返回相应的状态,格式如下:

  1. {
  2.     "code": "200",
  3.     "msg": "SUCCESS"
  4. }

 

 2.    返回相应的状态及数据,格式如下:

  1. {
  2.     "code": "200",
  3.     "msg": "查询成功",
  4.     "result": {
  5.         "id": 10,
  6.         "name": "张三",
  7.         "emil": "1234456@qq.com",
  8.         "phone": null,
  9.         "address": "测试地址"
  10.     }
  11. }

 状态代码定义如下:
   code : 请求处理状态

  •       200: 请求处理成功
  •       400: 请求处理失败
  •       500: 服务器内部错误
  •       401未认证(签名错误)
  •       404接口不存在

Springboot中我们可以使用泛型来定义统一的返回结果:
1. 先定义只返回状态的Result

  1. package com.example.demo.base;
  2. import com.alibaba.fastjson.JSON;
  3. public class Result {
  4. private String code;
  5. private String msg;
  6. public String getCode() {
  7. return code;
  8. }
  9. public void setCode(String code) {
  10. this.code = code;
  11. }
  12. public String getMsg() {
  13. return msg;
  14. }
  15. public void setMsg(String msg) {
  16. this.msg = msg;
  17. }
  18. @Override
  19. public String toString() {
  20. return JSON.toJSONString(this);
  21. }
  22. }

定义即有数据又有返回状态的DateResult

  1. package com.example.demo.base;
  2. import java.io.Serializable;
  3. public class DateResult<T> extends Result implements Serializable {
  4. private T result;
  5. public T getResult() {
  6. return (T) result;
  7. }
  8. public void setResult(T result) {
  9. this.result = result;
  10. }
  11. }

定义响应码枚举
 

  1. package com.example.demo.base;
  2. /**
  3. * 响应码枚举
  4. */
  5. public enum ResultCode {
  6. SUCCESS("200"), //成功
  7. FAIL("400"), //失败
  8. UNAUTHORIZED("401"), //未认证(签名错误)
  9. NOT_FOUND("404"), //接口不存在
  10. INTERNAL_SERVER_ERROR("500");//服务器内部错误
  11. private final String code;
  12. ResultCode(String code) {
  13. this.code = code;
  14. }
  15. public String code() {
  16. return code;
  17. }
  18. }

 在Controller类中使用方法如下:

  1. package com.example.demo.controller;
  2. import org.springframework.web.bind.annotation.PostMapping;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import com.example.demo.base.DateResult;
  7. import com.example.demo.base.Result;
  8. import com.example.demo.base.ResultCode;
  9. import com.example.demo.entity.User;
  10. @RestController
  11. @RequestMapping("/user")
  12. public class UserController {
  13. @PostMapping("/update")
  14. public Result updateUser(@RequestParam Integer id) {
  15. Result result = new Result();
  16. //相应的逻辑更新逻辑......... 假如更新成功
  17. result.setCode(ResultCode.SUCCESS.code());
  18. result.setMsg("更新成功");
  19. return result;
  20. }
  21. @RequestMapping("/detail")
  22. public DateResult<User> queryUser(@RequestParam Integer id) {
  23. User user = new User();
  24. user.setId(10);
  25. user.setName("张三");
  26. user.setEmil("1234456@qq.com");
  27. user.setAddress("测试地址");
  28. DateResult<User> dateResult = new DateResult<User>();
  29. dateResult.setCode(ResultCode.SUCCESS.code());
  30. dateResult.setMsg("查询成功");
  31. dateResult.setResult(user);
  32. return dateResult;
  33. }
  34. }

实体类User代码如下:

  1. package com.example.demo.entity;
  2. import java.io.Serializable;
  3. import com.alibaba.fastjson.JSON;
  4. public class User implements Serializable {
  5. private Integer id;
  6. private String name;
  7. private String emil;
  8. private String phone;
  9. private String address;
  10. public Integer getId() {
  11. return id;
  12. }
  13. public void setId(Integer id) {
  14. this.id = id;
  15. }
  16. public String getName() {
  17. return name;
  18. }
  19. public void setName(String name) {
  20. this.name = name;
  21. }
  22. public String getEmil() {
  23. return emil;
  24. }
  25. public void setEmil(String emil) {
  26. this.emil = emil;
  27. }
  28. public String getPhone() {
  29. return phone;
  30. }
  31. public void setPhone(String phone) {
  32. this.phone = phone;
  33. }
  34. public String getAddress() {
  35. return address;
  36. }
  37. public void setAddress(String address) {
  38. this.address = address;
  39. }
  40. @Override
  41. public String toString() {
  42. return JSON.toJSONString(this);
  43. }
  44. }

先测试没有数据的返回格式,测试结果如下:

无数据的返回格式

测试有返回数据的格式,测试结果如下:

如上的返回结果,我们需要在controller类中每次都需要new一个返回对象比较麻烦,我们可以新建一个ResultUtil

  1. package com.example.demo.base;
  2. /**
  3. * 响应结果生成工具
  4. */
  5. public class ResultUtil {
  6. private static final String DEFAULT_SUCCESS_MESSAGE = "SUCCESS";
  7. public static Result genSuccessResult() {
  8. Result result = new Result();
  9. result.setCode(ResultCode.SUCCESS.code());
  10. result.setMsg(DEFAULT_SUCCESS_MESSAGE);
  11. return result;
  12. }
  13. public static <T> DateResult<T> genSuccessResult(T data) {
  14. DateResult<T> dateResult = new DateResult<T>();
  15. dateResult.setCode(ResultCode.SUCCESS.code());
  16. dateResult.setMsg(DEFAULT_SUCCESS_MESSAGE);
  17. dateResult.setResult(data);
  18. return dateResult;
  19. }
  20. }

 controller 类的代码修改如下:

  1. package com.example.demo.controller;
  2. import org.springframework.web.bind.annotation.PostMapping;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import com.example.demo.base.DateResult;
  7. import com.example.demo.base.Result;
  8. import com.example.demo.base.ResultUtil;
  9. import com.example.demo.entity.User;
  10. @RestController
  11. @RequestMapping("/user")
  12. public class UserController {
  13. @PostMapping("/update")
  14. public Result updateUser(@RequestParam Integer id) {
  15. return ResultUtil.genSuccessResult();
  16. }
  17. @RequestMapping("/detail")
  18. public DateResult<User> queryUser(@RequestParam Integer id) {
  19. User user = new User();
  20. user.setId(10);
  21. user.setName("张三");
  22. user.setEmil("1234456@qq.com");
  23. user.setAddress("测试地址");
  24. return ResultUtil.genSuccessResult(user);
  25. }
  26. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/183980
推荐阅读
相关标签
  

闽ICP备14008679号