Jelajahi Sumber

删除主动捕获异常,异常统一进行处理

周浩 9 tahun lalu
induk
melakukan
89d058b24e

+ 40 - 64
hsweb-web-controller/src/main/java/org/hsweb/web/controller/GenericController.java

@@ -63,21 +63,17 @@ public abstract class GenericController<PO, PK> {
     @RequestMapping(method = RequestMethod.GET)
     @AccessLogger("查询列表")
     @Authorize(action = "R")
-    public ResponseMessage list(QueryParam param) {
+    public ResponseMessage list(QueryParam param) throws Exception {
         // 获取条件查询
-        try {
-            Object data;
-            if (!param.isPaging())//不分页
-                data = getService().select(param);
-            else
-                data = getService().selectPager(param);
-            return new ResponseMessage(true, data)
-                    .include(getPOType(), param.getIncludes())
-                    .exclude(getPOType(), param.getExcludes())
-                    .onlyData();
-        } catch (Exception e) {
-            return new ResponseMessage(false, e);
-        }
+        Object data;
+        if (!param.isPaging())//不分页
+            data = getService().select(param);
+        else
+            data = getService().selectPager(param);
+        return new ResponseMessage(true, data)
+                .include(getPOType(), param.getIncludes())
+                .exclude(getPOType(), param.getExcludes())
+                .onlyData();
     }
 
     /**
@@ -89,15 +85,11 @@ public abstract class GenericController<PO, PK> {
     @RequestMapping(value = "/{id}", method = RequestMethod.GET)
     @AccessLogger("查询明细")
     @Authorize(action = "R")
-    public ResponseMessage info(@PathVariable("id") PK id) {
-        try {
-            PO po = getService().selectByPk(id);
-            if (po == null)
-                return new ResponseMessage(false, "data is not found!", "404");
-            return new ResponseMessage(true, po);
-        } catch (Exception e) {
-            return new ResponseMessage(false, e);
-        }
+    public ResponseMessage info(@PathVariable("id") PK id) throws Exception {
+        PO po = getService().selectByPk(id);
+        if (po == null)
+            return new ResponseMessage(false, "data is not found!", "404");
+        return new ResponseMessage(true, po);
     }
 
 
@@ -110,13 +102,9 @@ public abstract class GenericController<PO, PK> {
     @RequestMapping(value = "/total", method = RequestMethod.GET)
     @AccessLogger("查询总数")
     @Authorize(action = "R")
-    public ResponseMessage total(QueryParam param) {
-        try {
-            // 获取条件查询
-            return new ResponseMessage(true, getService().total(param));
-        } catch (Exception e) {
-            return new ResponseMessage(false, e);
-        }
+    public ResponseMessage total(QueryParam param) throws Exception {
+        // 获取条件查询
+        return new ResponseMessage(true, getService().total(param));
     }
 
     /**
@@ -128,7 +116,7 @@ public abstract class GenericController<PO, PK> {
     @RequestMapping(method = RequestMethod.POST)
     @AccessLogger("新增")
     @Authorize(action = "C")
-    public ResponseMessage add(@RequestBody PO object) {
+    public ResponseMessage add(@RequestBody PO object) throws Exception {
         try {
             PK pk = getService().insert(object);
             return new ResponseMessage(true, pk);
@@ -146,13 +134,9 @@ public abstract class GenericController<PO, PK> {
     @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
     @AccessLogger("删除")
     @Authorize(action = "D")
-    public ResponseMessage delete(@PathVariable("id") PK id) {
-        try {
-            int number = getService().delete(id);
-            return new ResponseMessage(true, number);
-        } catch (Exception e) {
-            return new ResponseMessage(false, e);
-        }
+    public ResponseMessage delete(@PathVariable("id") PK id) throws Exception {
+        int number = getService().delete(id);
+        return new ResponseMessage(true, number);
     }
 
     /**
@@ -164,16 +148,12 @@ public abstract class GenericController<PO, PK> {
     @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
     @AccessLogger("修改")
     @Authorize(action = "U")
-    public ResponseMessage update(@PathVariable("id") PK id, @RequestBody(required = true) PO object) {
-        try {
-            if (object instanceof GenericPo) {
-                ((GenericPo) object).setU_id(id);
-            }
-            int number = getService().update(object);
-            return new ResponseMessage(true, number);
-        } catch (Exception e) {
-            return new ResponseMessage(false, e);
+    public ResponseMessage update(@PathVariable("id") PK id, @RequestBody(required = true) PO object) throws Exception {
+        if (object instanceof GenericPo) {
+            ((GenericPo) object).setU_id(id);
         }
+        int number = getService().update(object);
+        return new ResponseMessage(true, number);
     }
 
     /**
@@ -185,23 +165,19 @@ public abstract class GenericController<PO, PK> {
     @RequestMapping(method = RequestMethod.PUT)
     @AccessLogger("批量修改")
     @Authorize(action = "U")
-    public ResponseMessage update(@RequestBody(required = true) String json) {
-        try {
-            int number;
-            if (json.startsWith("[")) {
-                List<PO> datas = JSON.parseArray(json, getPOType());
-                number = getService().update(datas);
-            } else if (json.startsWith("{")) {
-                PO data = JSON.parseObject(json, getPOType());
-                List<PO> datas = new ArrayList<>();
-                datas.add(data);
-                number = getService().update(datas);
-            } else {
-                return new ResponseMessage(false, "数据错误");
-            }
-            return new ResponseMessage(true, number);
-        } catch (Exception e) {
-            return new ResponseMessage(false, e);
+    public ResponseMessage update(@RequestBody(required = true) String json) throws Exception {
+        int number;
+        if (json.startsWith("[")) {
+            List<PO> datas = JSON.parseArray(json, getPOType());
+            number = getService().update(datas);
+        } else if (json.startsWith("{")) {
+            PO data = JSON.parseObject(json, getPOType());
+            List<PO> datas = new ArrayList<>();
+            datas.add(data);
+            number = getService().update(datas);
+        } else {
+            return new ResponseMessage(false, "数据错误");
         }
+        return new ResponseMessage(true, number);
     }
 }

+ 3 - 3
hsweb-web-controller/src/main/java/org/hsweb/web/controller/config/ConfigController.java

@@ -137,19 +137,19 @@ public class ConfigController extends GenericController<Config, String> {
 
     @Override
     @RequestMapping(value = "/{id:.+}", method = RequestMethod.GET)
-    public ResponseMessage info(@PathVariable("id") String id) {
+    public ResponseMessage info(@PathVariable("id") String id)throws Exception {
         return super.info(id);
     }
 
     @Override
     @Authorize(module = "config", action = "C")
-    public ResponseMessage add(@RequestBody Config object) {
+    public ResponseMessage add(@RequestBody Config object)throws Exception {
         return super.add(object);
     }
 
     @Override
     @Authorize(module = "config", action = "U")
-    public ResponseMessage update(@PathVariable("id") String id, @RequestBody Config object) {
+    public ResponseMessage update(@PathVariable("id") String id, @RequestBody Config object)throws Exception {
         return super.update(id, object);
     }
 }

+ 16 - 20
hsweb-web-controller/src/main/java/org/hsweb/web/controller/resource/ResourcesController.java

@@ -43,7 +43,7 @@ public class ResourcesController extends GenericController<Resources, String> {
      */
     @Override
     @Authorize(role = Role.SYS_ROLE_ADMIN)
-    public ResponseMessage delete(@PathVariable("id") String id) {
+    public ResponseMessage delete(@PathVariable("id") String id)throws Exception {
         return super.delete(id);
     }
 
@@ -56,25 +56,21 @@ public class ResourcesController extends GenericController<Resources, String> {
     @RequestMapping(value = "/{id:^[0-9a-zA-Z]*$}", method = RequestMethod.GET)
     @ResponseBody
     @AccessLogger("获取资源信息")
-    public ResponseMessage info(@PathVariable("id") String id) {
-        try {
-            Resources resources;
-            //如果id长度为32,则尝试通过md5获取
-            if (id.length() == 32) {
-                resources = getService().selectByMd5(id);
-                if (resources == null)
-                    resources = getService().selectByPk(id);
-            } else
-                resources = resourcesService.selectByPk(id);
-            if (resources == null) {
-                return new ResponseMessage(false, "资源不存在!", "404");
-            } else {
-                if (resources.getStatus() != 1)
-                    return new ResponseMessage(false, "拒绝访问!", "502");
-                return new ResponseMessage(true, resources);
-            }
-        } catch (Exception e) {
-            return new ResponseMessage(false, e);
+    public ResponseMessage info(@PathVariable("id") String id) throws Exception {
+        Resources resources;
+        //如果id长度为32,则尝试通过md5获取
+        if (id.length() == 32) {
+            resources = getService().selectByMd5(id);
+            if (resources == null)
+                resources = getService().selectByPk(id);
+        } else
+            resources = resourcesService.selectByPk(id);
+        if (resources == null) {
+            return new ResponseMessage(false, "资源不存在!", "404");
+        } else {
+            if (resources.getStatus() != 1)
+                return new ResponseMessage(false, "拒绝访问!", "502");
+            return new ResponseMessage(true, resources);
         }
     }
 

+ 1 - 1
hsweb-web-controller/src/main/java/org/hsweb/web/controller/role/RoleController.java

@@ -33,7 +33,7 @@ public class RoleController extends GenericController<Role, String> {
     }
 
     @Override
-    public ResponseMessage list(QueryParam param) {
+    public ResponseMessage list(QueryParam param)throws Exception {
         return super.list(param).exclude(Role.class, "modules");
     }
 }

+ 8 - 12
hsweb-web-controller/src/main/java/org/hsweb/web/controller/user/UserController.java

@@ -35,7 +35,7 @@ public class UserController extends GenericController<User, String> {
 
     @Override
     @AccessLogger("获取列表")
-    public ResponseMessage list(QueryParam param) {
+    public ResponseMessage list(QueryParam param) throws Exception {
         param.excludes("password");
         return super.list(param)
                 .exclude(User.class, "password", "modules", "userRoles")
@@ -44,22 +44,18 @@ public class UserController extends GenericController<User, String> {
 
     @Override
     @AccessLogger("获取用户详情")
-    public ResponseMessage info(@PathVariable("id") String id) {
+    public ResponseMessage info(@PathVariable("id") String id) throws Exception {
         return super.info(id).exclude(User.class, "password", "modules");
     }
 
     @Override
     @AccessLogger("删除")
-    public ResponseMessage delete(@PathVariable("id") String id) {
-        try {
-            User user = getService().selectByPk(id);
-            if (user == null) return new ResponseMessage(false, "该用户不存在!", "404");
-            user.setStatus(-1);
-            getService().update(user);
-            return new ResponseMessage(true, "删除成功");
-        } catch (Exception e) {
-            return new ResponseMessage(false, e);
-        }
+    public ResponseMessage delete(@PathVariable("id") String id) throws Exception {
+        User user = getService().selectByPk(id);
+        if (user == null) return new ResponseMessage(false, "该用户不存在!", "404");
+        user.setStatus(-1);
+        getService().update(user);
+        return new ResponseMessage(true, "删除成功");
     }