Spring-Custom之spring-custom-web

Bertha 。 2021-11-05 08:32 365阅读 0赞

Spring-Custom之spring-custom-web

  Spring-Custom是基于springboot开发的一些组件,自定义的一些规则规范开发过程.。spring-custom-web主要是对VO、Controller(RestController)做的一些封装。

版本说明

基于springboot 2.1.4.RELEASE(这部分影响不大)

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.1.4.RELEASE</version>
  5. <relativePath/> <!-- lookup parent from repository -->
  6. </parent>

具体代码可以查看
https://gitee.com/cjx913/spring-custom-web.git

配置文件的配置项

  1. spring:
  2. custom:
  3. web:
  4. #返回的数据带有msg属性,默认是msg
  5. message: message
  6. #返回的数据带有code属性,默认是code
  7. code: code
  8. #表示路由,主要是对view的路径解析
  9. router:
  10. views:
  11. login: login #login.html对应的url是login
  12. register: register
  13. children:
  14. - path: manager
  15. children:
  16. - path: user
  17. views:
  18. add: user_add,userAdd,user.html # manager/user/add.html对应的url是/user_add和/userAdd和/user.html
  19. delete: user_delete
  20. - path: order
  21. views:
  22. add: order_add
  23. delete: order_delete

是入口类带上@EnableCustomWeb注解开启spring-custom-web功能

  1. @SpringBootApplication
  2. @RestController
  3. @EnableCustomWeb
  4. public class ResultTest {
  5. @Autowired
  6. private ResultBuilder resultBuilder;
  7. public static void main(String[] args) {
  8. SpringApplication.run(ResultTest.class, args);
  9. }
  10. @GetMapping("/test")
  11. public ResultEntity test(HttpSession session) {
  12. Result result = resultBuilder.build();
  13. result.addHeaders("header", "asdsadsda").addHeaders("header", "assafasggq12412")
  14. .setHeaders("header1", "asasdsadsad").setHeaders("header1", "a124123sasdsadsad")
  15. .addHeaders("sessionid", session.getId())
  16. .putBody("key", "asdasf")
  17. .putBody("name", "nasd");
  18. return result.resultEntity(StatusCode.SELECT_SUCCESS, "saagasdasd", HttpStatus.OK);
  19. }
  20. @PostMapping("/user")
  21. public ResultEntity user(@RequestBody UserBaseQueryVO userBaseQueryVO) {
  22. Result result = resultBuilder.build();
  23. User user = new User();
  24. user.setUsername("cjx913");
  25. user.setPassword("cjx913");
  26. User entity = userBaseQueryVO.getEntity();
  27. result.putBody("user", entity);
  28. return result.resultEntity();
  29. }
  30. }

ResultEntity
public class ResultEntity extends ResponseEntity {…}
Result是Map,做了一些封装

  1. public class Result extends HashMap <String, Object> {
  2. protected String DEFAULT_CODE = "code";
  3. protected String DEFAULT_MSG = "msg";
  4. Result() {
  5. }
  6. private MultiValueMap <String, String> headers = new HttpHeaders();
  7. public ResultEntity resultEntity() {
  8. return resultEntity(HttpStatus.OK);
  9. }
  10. public ResultEntity resultEntityInternalServerError() {
  11. return resultEntity(HttpStatus.INTERNAL_SERVER_ERROR);
  12. }
  13. public ResultEntity resultEntity(HttpStatus status) {
  14. return resultEntity(null, null, status);
  15. }
  16. public ResultEntity resultEntity(Integer code, String msg) {
  17. return resultEntity(code, msg, HttpStatus.OK);
  18. }
  19. public ResultEntity resultEntityInternalServerError(Integer code, String msg) {
  20. return resultEntity(code, msg, HttpStatus.INTERNAL_SERVER_ERROR);
  21. }
  22. public ResultEntity resultEntity(Integer code, String msg, HttpStatus status) {
  23. if (status == null) {
  24. throw new ResponseException("HttpStatus can not be null");
  25. }
  26. if (msg != null) {
  27. this.putBody(DEFAULT_MSG, msg);
  28. }
  29. if (code != null) {
  30. this.putBody(DEFAULT_CODE, code);
  31. }
  32. if (headers == null || headers.isEmpty()) {
  33. return new ResultEntity(this, status);
  34. } else {
  35. return new ResultEntity(this, headers, status);
  36. }
  37. }
  38. public Result setCode(Integer code) {
  39. putBody(DEFAULT_CODE, code);
  40. return this;
  41. }
  42. public Result setMsg(String msg) {
  43. putBody(DEFAULT_MSG, msg);
  44. return this;
  45. }
  46. public Result putBody(String key, Object value) {
  47. this.put(key, value);
  48. return this;
  49. }
  50. public Result removeBody(String key) {
  51. this.remove(key);
  52. return this;
  53. }
  54. public Result addHeaders(String key, String value) {
  55. this.headers.add(key, value);
  56. return this;
  57. }
  58. public Result setHeaders(String key, String value) {
  59. this.headers.set(key, value);
  60. return this;
  61. }
  62. public Result removeHeaders(String key) {
  63. this.headers.remove(key);
  64. return this;
  65. }
  66. public MultiValueMap <String, String> getHeaders() {
  67. return headers;
  68. }
  69. public Result setHeaders(MultiValueMap <String, String> headers) {
  70. this.headers = headers;
  71. return this;
  72. }
  73. public Result addHeaders(MultiValueMap <String, String> headers) {
  74. this.headers.addAll(headers);
  75. return this;
  76. }
  77. public String getDefaultCode() {
  78. return DEFAULT_CODE;
  79. }
  80. public void setDefaultCode(String defaultCode) {
  81. Integer code = (Integer) this.get(DEFAULT_CODE);
  82. if (code != null) {
  83. this.removeBody(DEFAULT_CODE);
  84. DEFAULT_CODE = defaultCode;
  85. this.setCode(code);
  86. } else {
  87. DEFAULT_CODE = defaultCode;
  88. }
  89. }
  90. public String getDefaultMsg() {
  91. return DEFAULT_MSG;
  92. }
  93. public void setDefaultMsg(String defaultMsg) {
  94. String msg = (String) this.get(DEFAULT_MSG);
  95. if (msg != null) {
  96. this.removeBody(DEFAULT_MSG);
  97. DEFAULT_MSG = defaultMsg;
  98. this.setMsg(msg);
  99. } else {
  100. DEFAULT_MSG = defaultMsg;
  101. }
  102. }
  103. }

自定义了一些状态码StatusCode

  1. package cn.cjx913.spring_custom.web;
  2. /** * 操作状态码 * @author cjx913 * @version 1.0 * @since 2.1.2.RELEASE */
  3. public class StatusCode {
  4. /** * 查询成功 */
  5. public static final Integer SELECT_SUCCESS = 101;
  6. /** * 查询成功,但无数据 */
  7. public static final Integer SELECT_SUCCESS_NO_DATA = 102;
  8. /** * 查询失败 */
  9. public static final Integer SELECT_FAIL = 103;
  10. /** * 插入成功 */
  11. public static final Integer INSERT_SUCCESS = 104;
  12. /** * 插入失败 */
  13. public static final Integer INSERT_FAIL = 105;
  14. /** * 更新成功 */
  15. public static final Integer UPDATE_SUCCESS = 106;
  16. /** * 更新失败 */
  17. public static final Integer UPDATE_FAIL = 107;
  18. /** * 删除成功 */
  19. public static final Integer DELETE_SUCCESS = 108;
  20. /** * 删除失败 */
  21. public static final Integer DELETE_FAIL = 109;
  22. /** * 登陆成功 */
  23. public static final Integer LOGIN_SUCCESS = 210;
  24. /** * 登陆失败 */
  25. public static final Integer LOGIN_FAIL = 211;
  26. /** * 登陆失败,用户不存在 */
  27. public static final Integer LOGIN_FAIL_UNREGISTER = 212;
  28. /** * 登陆失败,无效登录凭证 */
  29. public static final Integer LOGIN_FAIL_INVALID_CERTIFICATE = 213;
  30. /** * 登陆失败,未知登录类型 */
  31. public static final Integer LOGIN_FAIL_UNKONWN_LOGIN_TYPE = 214;
  32. /** * 注册成功 */
  33. public static final Integer REFISTER_SUCCESS = 220;
  34. /** * 注册失败 */
  35. public static final Integer REFISTER_FAIL = 221;
  36. /** * 注册失败,用户已存在 */
  37. public static final Integer REFISTER_FAIL_EXISTED_USER = 222;
  38. /** * 注册失败,保存用户数据错误 */
  39. public static final Integer REFISTER_FAIL_ERROR_SAVE_USER_DATA = 223;
  40. /** * 注册失败,未知注册类型 */
  41. public static final Integer REGISTER_FAIL_UNKONWN_REGISTER_TYPE = 224;
  42. /** * 有效的session */
  43. public static final Integer SESSION_VALID = 301;
  44. /** * 无效的session */
  45. public static final Integer SESSION_INVALID = 302;
  46. /** * 有效的token */
  47. public static final Integer TOKEN_VALID = 311;
  48. /** * 无效的token */
  49. public static final Integer TOKEN_INVALID = 312;
  50. /** * 有效的验证码 */
  51. public static final Integer VERIFICATION_CODE_VALID = 321;
  52. /** * 无效的验证码 */
  53. public static final Integer VERIFICATION_CODE_INVALID = 322;
  54. /** * 未知错误 */
  55. public static final Integer ERROR_UNKONWN = 900;
  56. /** * 请求错误 */
  57. public static final Integer ERROR_REQUSET = 910;
  58. /** * 请求参数错误 */
  59. public static final Integer ERROR_REQUSET_PARAMETER = 911;
  60. /** * 响应错误 */
  61. public static final Integer ERROR_RESPONSE = 920;
  62. /** * 文件上传错误 */
  63. public static final Integer ERROR_FILR_UPLOAD = 970;
  64. /** * 文件下载错误 */
  65. public static final Integer ERROR_FILR_DOWNLOAD = 980;
  66. /** * 全局异常 */
  67. public static final Integer ERROR_GLOBAL_EXCEPTIONS = 999;
  68. }

使用ResultBuilder来创建Result,使用@Autowrite注入的,能够使它跟配置文件的code,message一致

  1. @Autowired
  2. private ResultBuilder resultBuilder;
  3. ......
  4. Result result = resultBuilder.build();

Result提供转换成ResultEntity的方法

  1. public ResultEntity resultEntity() {
  2. return resultEntity(HttpStatus.OK);
  3. }
  4. public ResultEntity resultEntityInternalServerError() {
  5. return resultEntity(HttpStatus.INTERNAL_SERVER_ERROR);
  6. }
  7. public ResultEntity resultEntity(HttpStatus status) {
  8. return resultEntity(null, null, status);
  9. }
  10. public ResultEntity resultEntity(Integer code, String msg) {
  11. return resultEntity(code, msg, HttpStatus.OK);
  12. }
  13. public ResultEntity resultEntityInternalServerError(Integer code, String msg) {
  14. return resultEntity(code, msg, HttpStatus.INTERNAL_SERVER_ERROR);
  15. }
  16. public ResultEntity resultEntity(Integer code, String msg, HttpStatus status) {
  17. if (status == null) {
  18. throw new ResponseException("HttpStatus can not be null");
  19. }
  20. if (msg != null) {
  21. this.putBody(DEFAULT_MSG, msg);
  22. }
  23. if (code != null) {
  24. this.putBody(DEFAULT_CODE, code);
  25. }
  26. if (headers == null || headers.isEmpty()) {
  27. return new ResultEntity(this, status);
  28. } else {
  29. return new ResultEntity(this, headers, status);
  30. }
  31. }

还有对查询和结果做了一些规定

  1. package cn.cjx913.spring_custom.web.entity;
  2. import lombok.Getter;
  3. import lombok.Setter;
  4. import java.io.Serializable;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. @Setter
  8. @Getter
  9. /** * 查询参数封装 */
  10. public class BaseQueryVO<T> implements Serializable {
  11. protected T entity;
  12. /** * 数据有效性 */
  13. protected Boolean available;
  14. /** * 搜索文本 */
  15. protected String searchText;
  16. /** * 排序 */
  17. protected String sortName;
  18. /** * 排序 */
  19. protected String sortOrder = "asc";
  20. //分页
  21. /** * 分页查询的页码 */
  22. protected Integer pageNumber = 1;
  23. /** * 分页查询的每頁的数据 */
  24. protected Integer pageSize = 15;
  25. /** * 分页查询的每頁的数据 */
  26. protected Integer limit = 15;
  27. /** * 分页查询的起始号 */
  28. protected Integer offset = 0;
  29. protected Map <String, Object> parameters = new HashMap <>();
  30. public BaseQueryVO() {
  31. }
  32. public BaseQueryVO(T entity) {
  33. this.entity = entity;
  34. }
  35. }
  36. package cn.cjx913.spring_custom.web.entity;
  37. import lombok.Getter;
  38. import lombok.Setter;
  39. import org.springframework.util.CollectionUtils;
  40. import java.io.Serializable;
  41. import java.util.HashMap;
  42. import java.util.List;
  43. import java.util.Map;
  44. @Setter
  45. @Getter
  46. /** * 查询结果封装 */
  47. public class BaseResultVO<T> implements Serializable {
  48. protected T row;
  49. protected List <T> rows;
  50. /** * 查询得到总数据的行数 */
  51. protected Long total;
  52. /** * 增删改受影响的行数 */
  53. protected Long count;
  54. protected Double sum;
  55. protected Double average;
  56. protected Map <String, Object> results = new HashMap <>();
  57. public Boolean hasData() {
  58. return row != null ? true : false;
  59. }
  60. public Boolean hasDatas() {
  61. if (CollectionUtils.isEmpty(rows)) {
  62. return false;
  63. } else {
  64. return true;
  65. }
  66. }
  67. }

WebMvcCustomConfigurer

public class WebMvcCustomConfigurer implements WebMvcConfigurer{}
在这里处理router的,需要把他注入spring容器

发表评论

表情:
评论列表 (有 0 条评论,365人围观)

还没有评论,来说两句吧...

相关阅读