ResponseProvider.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the dreamlu.net developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: Chill 庄骞 (smallchill@163.com)
  16. */
  17. package org.springblade.gateway.provider;
  18. import java.util.HashMap;
  19. import java.util.Map;
  20. /**
  21. * 请求响应返回
  22. *
  23. * @author Chill
  24. */
  25. public class ResponseProvider {
  26. /**
  27. * 成功
  28. *
  29. * @param message 信息
  30. * @return
  31. */
  32. public static Map<String, Object> success(String message) {
  33. return response(200, message);
  34. }
  35. /**
  36. * 失败
  37. *
  38. * @param message 信息
  39. * @return
  40. */
  41. public static Map<String, Object> fail(String message) {
  42. return response(400, message);
  43. }
  44. /**
  45. * 未授权
  46. *
  47. * @param message 信息
  48. * @return
  49. */
  50. public static Map<String, Object> unAuth(String message) {
  51. return response(401, message);
  52. }
  53. /**
  54. * 服务器异常
  55. *
  56. * @param message 信息
  57. * @return
  58. */
  59. public static Map<String, Object> error(String message) {
  60. return response(500, message);
  61. }
  62. /**
  63. * 构建返回的JSON数据格式
  64. *
  65. * @param status 状态码
  66. * @param message 信息
  67. * @return
  68. */
  69. public static Map<String, Object> response(int status, String message) {
  70. Map<String, Object> map = new HashMap<>(16);
  71. map.put("code", status);
  72. map.put("msg", message);
  73. map.put("data", null);
  74. return map;
  75. }
  76. }