pert: 统一异常处理

dev
huangfeng 1 year ago
parent 5e4ebe4296
commit 6bd5592109

@ -25,48 +25,32 @@ public class BdzController extends BasicController {
@GetMapping("listAll") @GetMapping("listAll")
@ApiOperation("查询全部列表") @ApiOperation("查询全部列表")
public Response<List<Bdz>> listAll() { public Response<List<Bdz>> listAll() {
try { List<Bdz> result = service.listAll();
List<Bdz> result = service.listAll(); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("add") @PostMapping("add")
@ApiOperation("新增") @ApiOperation("新增")
public Response<Bdz> add(@Validated @RequestBody Bdz item) { public Response<Bdz> add(@Validated @RequestBody Bdz item) throws Exception {
try { Bdz result = service.add(item);
Bdz result = service.add(item); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("update") @PostMapping("update")
@ApiOperation("更新") @ApiOperation("更新")
public Response<String> update(@Validated @RequestBody Bdz item) { public Response<String> update(@Validated @RequestBody Bdz item) throws Exception {
try { if (item.getId() == null) {
if (item.getId() == null) { throw new Exception("ID不能为空!");
throw new Exception("ID不能为空!");
}
service.update(item);
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
} }
service.update(item);
return Response.success("OK");
} }
@PostMapping("delete") @PostMapping("delete")
@ApiOperation("删除") @ApiOperation("删除")
public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) { public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) throws Exception {
try { service.delete(id);
service.delete(id); return Response.success("OK");
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
} }

@ -42,68 +42,44 @@ public class IcdConfigController extends BasicController {
@GetMapping("list") @GetMapping("list")
@ApiOperation("查询全部类型列表") @ApiOperation("查询全部类型列表")
public Response<List<IcdFileConfig>> list(String iedName) { public Response<List<IcdFileConfig>> list(String iedName) throws Exception {
try { List<IcdFileConfig> result = configService.list(iedName);
List<IcdFileConfig> result = configService.list(iedName); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@GetMapping("iedList") @GetMapping("iedList")
@ApiOperation("查询IED列表") @ApiOperation("查询IED列表")
public Response<List<String>> iedList() { public Response<List<String>> iedList() {
try { List<String> result = configService.iedList();
List<String> result = configService.iedList(); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("update") @PostMapping("update")
@ApiOperation("更新ICD类型配置") @ApiOperation("更新ICD类型配置")
public Response<String> update(@RequestBody IcdFileConfig item) { public Response<String> update(@RequestBody IcdFileConfig item) {
try { configService.update(item);
configService.update(item); return Response.success("OK");
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("delete") @PostMapping("delete")
@ApiOperation("删除ICD类型配置") @ApiOperation("删除ICD类型配置")
public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) { public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) {
try { configService.delete(id);
configService.delete(id); return Response.success("OK");
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@GetMapping("tableList") @GetMapping("tableList")
@ApiOperation("查询data表名") @ApiOperation("查询data表名")
public Response<List<String>> tableList() { public Response<List<String>> tableList() throws Exception {
try { List<String> result = dataService.getDataTables();
List<String> result = dataService.getDataTables(); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@GetMapping("colList") @GetMapping("colList")
@ApiOperation("查询data表字段名") @ApiOperation("查询data表字段名")
public Response<List<String>> colList(String tableName) { public Response<List<String>> colList(String tableName) throws Exception {
try { List<String> result = dataService.getDataTableColumns(tableName);
List<String> result = dataService.getDataTableColumns(tableName); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
} }

@ -25,48 +25,32 @@ public class JgController extends BasicController {
@GetMapping("listAll") @GetMapping("listAll")
@ApiOperation("查询列表") @ApiOperation("查询列表")
public Response<List<Jg>> listAll(Integer bdzid) { public Response<List<Jg>> listAll(Integer bdzid) {
try { List<Jg> result = service.listAll(bdzid);
List<Jg> result = service.listAll(bdzid); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("add") @PostMapping("add")
@ApiOperation("新增") @ApiOperation("新增")
public Response<Jg> add(@Validated @RequestBody Jg item) { public Response<Jg> add(@Validated @RequestBody Jg item) throws Exception {
try { Jg result = service.add(item);
Jg result = service.add(item); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("update") @PostMapping("update")
@ApiOperation("更新") @ApiOperation("更新")
public Response<String> update(@Validated @RequestBody Jg item) { public Response<String> update(@Validated @RequestBody Jg item) throws Exception {
try { if (item.getId() == null) {
if (item.getId() == null) { throw new Exception("ID不能为空!");
throw new Exception("ID不能为空!");
}
service.update(item);
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
} }
service.update(item);
return Response.success("OK");
} }
@PostMapping("delete") @PostMapping("delete")
@ApiOperation("删除") @ApiOperation("删除")
public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) { public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) throws Exception {
try { service.delete(id);
service.delete(id); return Response.success("OK");
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
} }

@ -25,48 +25,32 @@ public class LxController extends BasicController {
@GetMapping("listAll") @GetMapping("listAll")
@ApiOperation("查询全部列表") @ApiOperation("查询全部列表")
public Response<List<Lx>> listAll() { public Response<List<Lx>> listAll() {
try { List<Lx> result = service.listAll();
List<Lx> result = service.listAll(); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("add") @PostMapping("add")
@ApiOperation("新增") @ApiOperation("新增")
public Response<Lx> add(@Validated @RequestBody Lx item) { public Response<Lx> add(@Validated @RequestBody Lx item) throws Exception {
try { Lx result = service.add(item);
Lx result = service.add(item); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("update") @PostMapping("update")
@ApiOperation("更新") @ApiOperation("更新")
public Response<String> update(@Validated @RequestBody Lx item) { public Response<String> update(@Validated @RequestBody Lx item) throws Exception {
try { if (item.getId() == null) {
if (item.getId() == null) { throw new Exception("ID不能为空!");
throw new Exception("ID不能为空!");
}
service.update(item);
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
} }
service.update(item);
return Response.success("OK");
} }
@PostMapping("delete") @PostMapping("delete")
@ApiOperation("删除") @ApiOperation("删除")
public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) { public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) {
try { service.delete(id);
service.delete(id); return Response.success("OK");
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
} }

@ -28,59 +28,39 @@ public class ModevController extends BasicController {
@GetMapping("listAll") @GetMapping("listAll")
@ApiOperation("查询列表") @ApiOperation("查询列表")
public Response<List<Modev>> listAll(@NotNull(message = "主设备编号不能缺少") @Param("zsbid") Integer zsbid) { public Response<List<Modev>> listAll(@NotNull(message = "主设备编号不能缺少") @Param("zsbid") Integer zsbid) {
try { List<Modev> result = service.listAll(zsbid);
List<Modev> result = service.listAll(zsbid); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("add") @PostMapping("add")
@ApiOperation("新增") @ApiOperation("新增")
public Response<Modev> add(@Validated @RequestBody Modev item) { public Response<Modev> add(@Validated @RequestBody Modev item) throws Exception {
try { Modev result = service.add(item);
Modev result = service.add(item); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("update") @PostMapping("update")
@ApiOperation("更新") @ApiOperation("更新")
public Response<String> update(@Validated @RequestBody Modev item) { public Response<String> update(@Validated @RequestBody Modev item) throws Exception {
try { if (item.getId() == null) {
if (item.getId() == null) { throw new Exception("ID不能为空!");
throw new Exception("ID不能为空!");
}
service.update(item);
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
} }
service.update(item);
return Response.success("OK");
} }
@PostMapping("delete") @PostMapping("delete")
@ApiOperation("删除") @ApiOperation("删除")
public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") @Param("id") Integer id) { public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") @Param("id") Integer id) {
try { service.delete(id);
service.delete(id); return Response.success("OK");
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("detail") @PostMapping("detail")
@ApiOperation("详情") @ApiOperation("详情")
public Response<Modev> detail(@Validated @NotNull(message = "ID不能为空!") Integer id) { public Response<Modev> detail(@Validated @NotNull(message = "ID不能为空!") Integer id) throws Exception {
try { Modev detail = service.detail(id);
Modev detail = service.detail(id); return Response.success(detail);
return Response.success(detail);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
} }

@ -25,59 +25,39 @@ public class ModevTypeController extends BasicController {
@GetMapping("listAll") @GetMapping("listAll")
@ApiOperation("查询列表") @ApiOperation("查询列表")
public Response<List<ModevType>> listAll() { public Response<List<ModevType>> listAll() {
try { List<ModevType> result = service.listAll();
List<ModevType> result = service.listAll(); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("add") @PostMapping("add")
@ApiOperation("新增") @ApiOperation("新增")
public Response<ModevType> add(@Validated @RequestBody ModevType item) { public Response<ModevType> add(@Validated @RequestBody ModevType item) throws Exception {
try { ModevType result = service.add(item);
ModevType result = service.add(item); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("update") @PostMapping("update")
@ApiOperation("更新") @ApiOperation("更新")
public Response<String> update(@Validated @RequestBody ModevType item) { public Response<String> update(@Validated @RequestBody ModevType item) throws Exception {
try { if (item.getId() == null) {
if (item.getId() == null) { throw new Exception("ID不能为空!");
throw new Exception("ID不能为空!");
}
service.update(item);
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
} }
service.update(item);
return Response.success("OK");
} }
@PostMapping("delete") @PostMapping("delete")
@ApiOperation("删除") @ApiOperation("删除")
public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) { public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) {
try { service.delete(id);
service.delete(id); return Response.success("OK");
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("detail") @PostMapping("detail")
@ApiOperation("详情") @ApiOperation("详情")
public Response<ModevType> detail(@Validated @NotNull(message = "ID不能为空!") Integer id) { public Response<ModevType> detail(@Validated @NotNull(message = "ID不能为空!") Integer id) throws Exception {
try { ModevType detail = service.detail(id);
ModevType detail = service.detail(id); return Response.success(detail);
return Response.success(detail);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
} }

@ -40,70 +40,48 @@ public class NiecSensorController extends BasicController {
@ApiParam("每页数量") @RequestParam(value = "pageSize", required = false) Integer pageSize) { @ApiParam("每页数量") @RequestParam(value = "pageSize", required = false) Integer pageSize) {
pageNum = this.initPageNum(pageNum); pageNum = this.initPageNum(pageNum);
pageSize = this.initPageSize(pageSize); pageSize = this.initPageSize(pageSize);
try { Page<NiecSensor> result = service.list(pageNum, pageSize);
Page<NiecSensor> result = service.list(pageNum, pageSize); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@GetMapping("listAll") @GetMapping("listAll")
@ApiOperation("查询全部列表") @ApiOperation("查询全部列表")
public Response<List<NiecSensor>> listAll() { public Response<List<NiecSensor>> listAll() {
try { List<NiecSensor> result = service.listAll();
List<NiecSensor> result = service.listAll(); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@GetMapping("getTree") @GetMapping("getTree")
@ApiOperation("查询树") @ApiOperation("查询树")
public Response<List<NiecSensor>> getTree() { public Response<List<NiecSensor>> getTree() {
try { List<NiecSensor> result = service.getTree();
List<NiecSensor> result = service.getTree(); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
log.error("getTree Exception.", ex);
return Response.fail(ex.getMessage());
}
} }
@GetMapping("detail") @GetMapping("detail")
@ApiOperation("查询单个装置采集到的数据") @ApiOperation("查询单个装置采集到的数据")
public Response<SensorDetail> getDetail(@Validated ConditionModel model) { public Response<SensorDetail> getDetail(@Validated ConditionModel model) throws Exception {
try { if (model.getPageNum() != null || model.getPageSize() != null) {
if (model.getPageNum() != null || model.getPageSize() != null) { model.setPageNum(this.initPageNum(model.getPageNum()));
model.setPageNum(this.initPageNum(model.getPageNum())); model.setPageSize(this.initPageSize(model.getPageSize()));
model.setPageSize(this.initPageSize(model.getPageSize()));
}
SensorDetail result = service.getDetail(model);
return Response.success(result);
} catch (Exception ex) {
log.error("getDetail Exception.", ex);
return Response.fail(ex.getMessage());
} }
SensorDetail result = service.getDetail(model);
return Response.success(result);
} }
@GetMapping("export") @GetMapping("export")
@ApiOperation("导出单个装置采集到的数据") @ApiOperation("导出单个装置采集到的数据")
public void export(@Validated ConditionModel model, HttpServletResponse response) { public void export(@Validated ConditionModel model, HttpServletResponse response) throws Exception {
try { if (model.getPageNum() != null || model.getPageSize() != null) {
if (model.getPageNum() != null || model.getPageSize() != null) { model.setPageNum(this.initPageNum(model.getPageNum()));
model.setPageNum(this.initPageNum(model.getPageNum())); model.setPageSize(this.initPageSize(model.getPageSize()));
model.setPageSize(this.initPageSize(model.getPageSize()));
}
SensorDetail<Map<String, Object>> detail = service.getDetail(model);
response.setHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(detail.getSensor().getName() + ".xlsx", "UTF-8"));
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
reportService.exportSensor(detail, response.getOutputStream());
} catch (Exception ex) {
log.error("exportDetail Exception.", ex);
} }
SensorDetail<Map<String, Object>> detail = service.getDetail(model);
response.setHeader("Content-Disposition", "attachment; filename="
+ URLEncoder.encode(detail.getSensor().getName() + ".xlsx", "UTF-8"));
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
reportService.exportSensor(detail, response.getOutputStream());
} }
} }

@ -24,49 +24,33 @@ public class ZsbController extends BasicController {
@GetMapping("listAll") @GetMapping("listAll")
@ApiOperation("查询全部列表") @ApiOperation("查询全部列表")
public Response<List<Zsb>> listAll(Integer jgid) { public Response<List<Zsb>> listAll(Integer jgid) throws Exception {
try { List<Zsb> result = service.listAll(jgid);
List<Zsb> result = service.listAll(jgid); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("add") @PostMapping("add")
@ApiOperation("新增") @ApiOperation("新增")
public Response<Zsb> add(@Validated @RequestBody Zsb item) { public Response<Zsb> add(@Validated @RequestBody Zsb item) throws Exception {
try { Zsb result = service.add(item);
Zsb result = service.add(item); return Response.success(result);
return Response.success(result);
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
@PostMapping("update") @PostMapping("update")
@ApiOperation("更新") @ApiOperation("更新")
public Response<String> update(@Validated @RequestBody Zsb item) { public Response<String> update(@Validated @RequestBody Zsb item) throws Exception {
try { if (item.getId() == null) {
if (item.getId() == null) { throw new Exception("ID不能为空!");
throw new Exception("ID不能为空!");
}
service.update(item);
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
} }
service.update(item);
return Response.success("OK");
} }
@PostMapping("delete") @PostMapping("delete")
@ApiOperation("删除") @ApiOperation("删除")
public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) { public Response<String> delete(@Validated @NotNull(message = "ID不能为空!") Integer id) throws Exception {
try { service.delete(id);
service.delete(id); return Response.success("OK");
return Response.success("OK");
} catch (Exception ex) {
return Response.fail(ex.getMessage());
}
} }
} }

@ -1,6 +1,7 @@
package com.xydl.cac.exception; package com.xydl.cac.exception;
import com.xydl.cac.model.Response; import com.xydl.cac.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError; import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
@ -9,6 +10,7 @@ import org.springframework.web.bind.annotation.RestControllerAdvice;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@RestControllerAdvice @RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler { public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class) @ExceptionHandler(MethodArgumentNotValidException.class)
@ -21,6 +23,7 @@ public class GlobalExceptionHandler {
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public Response<String> handleException(Exception ex) { public Response<String> handleException(Exception ex) {
log.error("", ex);
String message = ex.getMessage(); String message = ex.getMessage();
return Response.fail(message); return Response.fail(message);
} }

Loading…
Cancel
Save