Bladeren bron

Merge remote-tracking branch 'origin/develop' into develop

tianyunperfect 4 jaren geleden
bovenliggende
commit
8735d99ed7

+ 1 - 0
.gitignore

@@ -46,3 +46,4 @@ com_crashlytics_export_strings.xml
 crashlytics.properties
 crashlytics-build.properties
 fabric.properties
+!/.github/workflows/

+ 5 - 1
CHANGELOG.md

@@ -1,5 +1,9 @@
 # 版本升级日志
 
+## 1.6.6 - 2020年12月19日
+
+- 封装controller返回值
+
 ## 1.6.5 - 2020年11月11日
 
 - 更新打包方式
@@ -26,4 +30,4 @@
 ## 1.6.0 - 2020年8月27日
 
 
-- test @田云
+- test @田云

+ 7 - 7
springboot-common/src/main/java/com/alvin/common/entity/Result.java

@@ -22,7 +22,7 @@ public class Result<T> implements Serializable {
     private T data;
 
     public static <T> Result<T> result(Boolean flag, ResultCode resultCode, T data) {
-        return new Result<T>(flag, resultCode.getCode(), resultCode.getMessage(), data);
+        return new Result<>(flag, resultCode.getCode(), resultCode.getMessage(), data);
     }
 
     //region success封装
@@ -42,11 +42,11 @@ public class Result<T> implements Serializable {
 
     //region failure封装
     public static <T> Result<T> failure(Integer code, String message) {
-        return new Result<T>(false, code, message, null);
+        return new Result<>(false, code, message, null);
     }
 
     public static <T> Result<T> failure(ResultCode resultCode) {
-        return new Result<T>(false, resultCode.getCode(), resultCode.getMessage(), null);
+        return new Result<>(false, resultCode.getCode(), resultCode.getMessage(), null);
     }
 
     public static <T> Result<T> failure() {
@@ -55,7 +55,7 @@ public class Result<T> implements Serializable {
     //endregion
 
     public static <T> Result<T> byObject(T t) {
-        return byObject(t,ResultCode.UNKNOW);
+        return byObject(t,ResultCode.UNKNOWN);
     }
     /**
      * 通过对象输出结果
@@ -77,7 +77,7 @@ public class Result<T> implements Serializable {
     }
 
     public static void main(String[] args) {
-        System.out.println(Result.byObject(null, ResultCode.UNKNOW));
-        System.out.println(Result.byObject(false, ResultCode.UNKNOW));
+        System.out.println(Result.byObject(null, ResultCode.UNKNOWN));
+        System.out.println(Result.byObject(false, ResultCode.UNKNOWN));
     }
-}
+}

+ 6 - 9
springboot-common/src/main/java/com/alvin/common/entity/ResultCode.java

@@ -2,17 +2,13 @@ package com.alvin.common.entity;
 
 public enum ResultCode {
     // 基本
-    SUCCESS(1, "success"),
+    SUCCESS(0, "success"),
     FAIL(-1, "fail"),
-    UNKNOW(-2, "unknow"),
+    UNKNOWN(-2, "unknown"),
+
     ;
-    /**
-     * 代码
-     */
+
     private Integer code;
-    /**
-     * 消息
-     */
     private String message;
 
     public Integer getCode() {
@@ -35,4 +31,5 @@ public enum ResultCode {
         this.code = code;
         this.message = message;
     }
-}
+
+}

+ 7 - 3
springboot-dao/pom.xml

@@ -27,8 +27,8 @@
 
         <!--common-->
         <dependency>
-            <groupId>com.alvin</groupId>
-            <artifactId>common</artifactId>
+            <groupId>com.alvin.springboot</groupId>
+            <artifactId>springboot-common</artifactId>
             <version>1.0-SNAPSHOT</version>
         </dependency>
         <!--mysql-->
@@ -37,6 +37,10 @@
             <artifactId>mysql-connector-java</artifactId>
             <version>8.0.16</version>
         </dependency>
+        <dependency>
+            <groupId>org.projectlombok</groupId>
+            <artifactId>lombok</artifactId>
+        </dependency>
     </dependencies>
     <build>
         <plugins>
@@ -66,4 +70,4 @@
             </plugin>
         </plugins>
     </build>
-</project>
+</project>

+ 7 - 1
springboot-main/pom.xml

@@ -69,6 +69,12 @@
             <artifactId>springboot-dao</artifactId>
             <version>1.0-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>com.alvin.springboot</groupId>
+            <artifactId>springboot-common</artifactId>
+            <version>1.0-SNAPSHOT</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 
-</project>
+</project>

+ 6 - 6
springboot-main/src/main/java/com/alvin/handle/ExceptionHandle.java → springboot-main/src/main/java/com/alvin/advice/ExceptionAdvice.java

@@ -1,4 +1,4 @@
-package com.alvin.handle;
+package com.alvin.advice;
 
 import com.alvin.common.entity.Result;
 import com.alvin.common.entity.ResultCode;
@@ -15,18 +15,18 @@ import org.springframework.web.bind.annotation.ResponseBody;
  */
 @ControllerAdvice
 @Slf4j
-public class ExceptionHandle {
+public class ExceptionAdvice {
 
     /**
      * 统一异常处理
-     * @param e
-     * @return
+     * @param e 异常
+     * @return Result
      */
     @ResponseBody
     @ExceptionHandler
     public Result exceptionHandle(Exception e) {
         e.printStackTrace();
         log.error("系统异常:{}", e.toString());
-        return Result.failure(ResultCode.UNKNOW.ordinal(), e.getMessage());
+        return Result.failure(ResultCode.UNKNOWN.ordinal(), e.getMessage());
     }
-}
+}

+ 69 - 0
springboot-main/src/main/java/com/alvin/advice/ResponseResultAdvice.java

@@ -0,0 +1,69 @@
+package com.alvin.advice;
+
+import com.alvin.annotation.ResponseResult;
+import com.alvin.common.entity.Result;
+import com.alvin.common.entity.ResultCode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.core.MethodParameter;
+import org.springframework.http.MediaType;
+import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.http.server.ServerHttpRequest;
+import org.springframework.http.server.ServerHttpResponse;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.context.request.RequestContextHolder;
+import org.springframework.web.context.request.ServletRequestAttributes;
+import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
+
+import javax.servlet.http.HttpServletRequest;
+import java.lang.annotation.Annotation;
+
+/**
+ * @Description 封装返回的数据
+ * @Author 田云
+ * @Date 2020/12/19 11:44
+ * @Version 1.0
+ */
+@ControllerAdvice
+public class ResponseResultAdvice implements ResponseBodyAdvice<Object> {
+
+    /**
+     * 请求中是否包含了 响应需要被包装的标记,如果没有,则直接返回,不需要重写返回体
+     *
+     * @param methodParameter
+     * @param aClass
+     * @return
+     */
+    @Override
+    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
+        ResponseResult annotation = methodParameter.getMethod().getDeclaringClass().getAnnotation(ResponseResult.class);
+        //先判断类上面有没有注释,再判断方法上有没有注释
+        if (annotation == null) {
+            annotation = methodParameter.getMethod().getAnnotation(ResponseResult.class);
+        }
+        return annotation == null ? false : true;
+    }
+
+    /**
+     * 对 响应体 进行包装; 除此之外还可以对响应体进行统一的加密、签名等
+     *
+     * @param responseBody 请求的接口方法执行后得到返回值(返回响应)
+     */
+    @Override
+    public Object beforeBodyWrite(Object responseBody, MethodParameter methodParameter,
+                                  MediaType mediaType,
+                                  Class<? extends HttpMessageConverter<?>> aClass,
+                                  ServerHttpRequest serverHttpRequest,
+                                  ServerHttpResponse serverHttpResponse) {
+        //自定义的错误异常
+        if (responseBody instanceof ResultCode) {
+            ResultCode code = (ResultCode) responseBody;
+            if (code == ResultCode.SUCCESS) {
+                return Result.success();
+            } else {
+                return Result.failure(code);
+            }
+        }
+        return Result.byObject(responseBody);
+    }
+}

+ 13 - 0
springboot-main/src/main/java/com/alvin/annotation/ResponseResult.java

@@ -0,0 +1,13 @@
+package com.alvin.annotation;
+
+import java.lang.annotation.*;
+
+/**
+ * 标记方法返回值需要进行包装的 自定义注解
+ * @author 田云
+ */
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@Documented
+public @interface ResponseResult {
+}

+ 7 - 4
springboot-main/src/main/java/com/alvin/controller/AppController.java

@@ -1,5 +1,6 @@
 package com.alvin.controller;
 
+import com.alvin.annotation.ResponseResult;
 import com.alvin.common.entity.PageResult;
 import com.alvin.common.entity.Result;
 import com.alvin.entity.User;
@@ -13,6 +14,7 @@ import java.util.Collections;
 @Slf4j
 @RestController
 @RequestMapping("/user")
+@ResponseResult
 public class AppController {
 
     /**
@@ -20,22 +22,23 @@ public class AppController {
      *
      * @return {@link Result<User>}
      */
+    @ResponseResult
     @GetMapping("/findOne")
-    public Result<User> findOne() {
+    public User findOne() {
         User user = new User();
         user.setName("小米");
         user.setAge(18);
         log.info(user.toString());
-        return Result.success(user);
+        return user;
     }
 
     @GetMapping("/queryPage")
-    public Result<PageResult<User>> queryPage() {
+    public PageResult<User> queryPage() {
         User user = new User();
         user.setName("小米");
         user.setAge(18);
         PageResult<User> pageResult = new PageResult<>(1, 10, 100L, Collections.singletonList(user));
-        return Result.success(pageResult);
+        return pageResult;
     }
 
 }

+ 8 - 8
springboot-main/src/test/java/com/alvin/controller/AppControllerTest.java

@@ -12,13 +12,13 @@ import java.util.Date;
 public class AppControllerTest extends TestBase {
 
     @Test
-    public void testFindOne() throws Exception {
-        mockMvc.perform(
-                MockMvcRequestBuilders.get("/user/findOne")
-                .contentType(MediaType.APPLICATION_JSON_UTF8)
-        )
-                .andExpect(MockMvcResultMatchers.status().isOk())
-                .andReturn();
+    public void testFindOne() {
+        //mockMvc.perform(
+        //        MockMvcRequestBuilders.get("/user/findOne")
+        //        .contentType(MediaType.APPLICATION_JSON_UTF8)
+        //)
+        //        .andExpect(MockMvcResultMatchers.status().isOk())
+        //        .andReturn();
         //System.out.println(mvcResult.getResponse().getContentAsString());
     }
 
@@ -26,4 +26,4 @@ public class AppControllerTest extends TestBase {
     public void testLog() {
         log.info(new Date().toString());
     }
-}
+}