소스 검색

增加全局接口数据类型, 增加统一异常处理

wuheng 1 년 전
부모
커밋
5a31bf609d

+ 28 - 0
common/src/main/java/com/koobietech/eas/common/exception/EasException.java

@@ -0,0 +1,28 @@
+package com.koobietech.eas.common.exception;
+
+public class EasException extends RuntimeException{
+    private int code=200;
+    private String msg;
+
+    public EasException(String msg) {
+        super(msg);
+        this.msg = msg;
+    }
+
+    public EasException(String msg, int code) {
+        super(msg);
+        this.msg = msg;
+        this.code = code;
+    }
+
+    public EasException(String msg, Throwable e) {
+        super(msg, e);
+        this.msg = msg;
+    }
+
+    public EasException(String msg, int code, Throwable e) {
+        super(msg, e);
+        this.msg = msg;
+        this.code = code;
+    }
+}

+ 71 - 0
common/src/main/java/com/koobietech/eas/common/result/JsonPageResult.java

@@ -0,0 +1,71 @@
+package com.koobietech.eas.common.result;
+
+import java.io.Serializable;
+
+public class JsonPageResult<T> implements Serializable {
+    private boolean status = false;
+    private String msg = "";
+    private T data = null;
+    private Long total = 0L;
+
+    public static JsonPageResult data(PageData data , String message  ){
+        JsonPageResult result = JsonPageResult.ok();
+        result.setTotal(data.getTotal());
+        result.setData(data.getData());
+        result.setMsg(message);
+        return result;
+    }
+
+    public static JsonPageResult data( PageData data  ){
+        JsonPageResult result = JsonPageResult.ok();
+        result.setData(data.getData());
+        result.setTotal(data.getTotal());
+        return result;
+    }
+
+    public static JsonPageResult ok(){
+        JsonPageResult result = new JsonPageResult();
+        result.setStatus(true);
+        result.setMsg("操作成功");
+        return result;
+    }
+
+    public static JsonPageResult fail( String message ){
+        JsonPageResult result = new JsonPageResult();
+        result.setStatus(false);
+        result.setMsg("操作失败");
+        return result;
+    }
+
+    public Long getTotal() {
+        return total;
+    }
+
+    public void setTotal(Long total) {
+        this.total = total;
+    }
+
+    public boolean getStatus() {
+        return status;
+    }
+
+    public void setStatus(boolean status) {
+        this.status = status;
+    }
+
+    public String getMsg() {
+        return msg;
+    }
+
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+
+    public T getData() {
+        return data;
+    }
+
+    public void setData(T data) {
+        this.data = data;
+    }
+}

+ 122 - 0
common/src/main/java/com/koobietech/eas/common/result/JsonResult.java

@@ -0,0 +1,122 @@
+package com.koobietech.eas.common.result;
+
+import java.io.Serializable;
+
+public class JsonResult implements Serializable {
+    private boolean status = false;
+    private String msg = "";
+    private Object data = null;
+
+    /**
+     * 成功
+     * @return
+     */
+    public static JsonResult data(Object data , String message  ){
+        JsonResult result = JsonResult.ok();
+        result.setData(data);
+        result.setMsg(message);
+        return result;
+    }
+
+    /**
+     * 成功
+     * @return
+     */
+    public static JsonResult data( Object data  ){
+        JsonResult result = JsonResult.ok();
+        result.setData(data);
+        return result;
+    }
+
+    /**
+     * 成功
+     * @return
+     */
+    public static JsonResult ok(){
+        JsonResult result = new JsonResult();
+        result.setStatus(true);
+        result.setMsg("操作成功");
+        return result;
+    }
+
+    /**
+     * 成功
+     * @return
+     */
+    public static JsonResult ok( String message ){
+        JsonResult result = new JsonResult();
+        result.setStatus(true);
+        result.setMsg(message);
+        return result;
+    }
+
+    /**
+     * 失败
+     * @return
+     */
+    public static JsonResult fail(){
+        JsonResult result = new JsonResult();
+        result.setStatus(false);
+        result.setMsg("操作失败");
+        return result;
+    }
+
+    /**
+     * 失败
+     * @return
+     */
+    public static JsonResult fail( Object data ){
+        JsonResult result = new JsonResult();
+        result.setStatus(false);
+        result.setData(data);
+        return result;
+    }
+
+    /**
+     * 失败
+     * @return
+     */
+    public static JsonResult fail( Object data, String msg ){
+        JsonResult result = new JsonResult();
+        result.setStatus(false);
+        result.setMsg(msg);
+        result.setData(data);
+        return result;
+    }
+
+    /**
+     * 失败
+     * @return
+     */
+    public static JsonResult fail( String message ){
+        JsonResult result = new JsonResult();
+        result.setStatus(false);
+        result.setMsg(message);
+        return result;
+    }
+
+
+    public boolean getStatus() {
+        return status;
+    }
+
+    public void setStatus(boolean status) {
+        this.status = status;
+    }
+
+    public String getMsg() {
+        return msg;
+    }
+
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+
+    public Object getData() {
+        return data;
+    }
+
+    public void setData(Object data) {
+        this.data = data;
+    }
+}

+ 17 - 0
common/src/main/java/com/koobietech/eas/common/result/PageData.java

@@ -0,0 +1,17 @@
+package com.koobietech.eas.common.result;
+
+import lombok.Data;
+
+import java.util.List;
+
+@Data
+public class PageData {
+    private long total = 0;
+    private List data = null;
+    public static PageData init(List data, long total){
+        PageData ret = new PageData();
+        ret.setData(data);
+        ret.setTotal(total);
+        return ret;
+    }
+}

+ 6 - 0
controller/pom.xml

@@ -33,6 +33,12 @@
             <artifactId>mbg</artifactId>
             <version>${project.version}</version>
         </dependency>
+        <dependency>
+            <groupId>com.koobietech.eas</groupId>
+            <artifactId>common</artifactId>
+            <version>0.0.1-SNAPSHOT</version>
+            <scope>compile</scope>
+        </dependency>
     </dependencies>
 
     <dependencyManagement>

+ 19 - 0
controller/src/main/java/com/koobietech/eas/config/ExceptionAdvice.java

@@ -0,0 +1,19 @@
+package com.koobietech.eas.config;
+
+import com.koobietech.eas.common.result.JsonResult;
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.ResponseStatus;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
+
+@RestControllerAdvice
+public class ExceptionAdvice {
+    @ResponseBody
+    @ResponseStatus(HttpStatus.OK)
+    @ExceptionHandler(Exception.class)
+    public JsonResult exceptionHandler(Exception e){
+        e.printStackTrace();
+        return JsonResult.fail(e.getMessage());
+    }
+}