异常捕获调整

This commit is contained in:
shikong 2023-09-07 16:45:15 +08:00
parent 8db1726e10
commit d4ae48d963
4 changed files with 18 additions and 7 deletions

View File

@ -5,6 +5,8 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import java.io.IOException;
/** /**
* 全局异常处理类 * 全局异常处理类
* *
@ -13,7 +15,7 @@ import org.springframework.web.bind.annotation.ExceptionHandler;
@Slf4j @Slf4j
@ControllerAdvice @ControllerAdvice
public class BasicExceptionAdvice { public class BasicExceptionAdvice {
@ExceptionHandler(Exception.class) @ExceptionHandler(IOException.class)
public void exception(HttpServletRequest request, Exception e) { public void exception(HttpServletRequest request, Exception e) {
if(request.getRequestURI().equals("/video")){ if(request.getRequestURI().equals("/video")){
return; return;

View File

@ -1,5 +1,6 @@
package cn.skcks.docking.gb28181.wvp.advice; package cn.skcks.docking.gb28181.wvp.advice;
import cn.skcks.docking.gb28181.common.json.JsonException;
import cn.skcks.docking.gb28181.common.json.JsonResponse; import cn.skcks.docking.gb28181.common.json.JsonResponse;
import jakarta.validation.ConstraintViolationException; import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -72,6 +73,11 @@ public class ExceptionAdvice {
return JsonResponse.error("参数异常"); return JsonResponse.error("参数异常");
} }
@ExceptionHandler(JsonException.class)
public JsonResponse<String> handleJsonException(JsonException e){
return JsonResponse.error(e.getMessage());
}
@ExceptionHandler(Exception.class) @ExceptionHandler(Exception.class)
public JsonResponse<String> exception(Exception e) { public JsonResponse<String> exception(Exception e) {
e.printStackTrace(); e.printStackTrace();

View File

@ -41,7 +41,7 @@ public class DeviceController {
} }
@PostJson("/add") @PostJson("/add")
public JsonResponse<Boolean> addDevice(@RequestBody AddDeviceDTO dto){ public JsonResponse<Boolean> addDevice(@RequestBody AddDeviceDTO dto) {
return JsonResponse.success(deviceService.addDevice(DeviceDTOConvertor.INSTANCE.dto2dao(dto))); return JsonResponse.success(deviceService.addDevice(DeviceDTOConvertor.INSTANCE.dto2dao(dto)));
} }
} }

View File

@ -1,5 +1,6 @@
package cn.skcks.docking.gb28181.wvp.service.device; package cn.skcks.docking.gb28181.wvp.service.device;
import cn.skcks.docking.gb28181.common.json.JsonException;
import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.mapper.WvpProxyDeviceDynamicSqlSupport; import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.mapper.WvpProxyDeviceDynamicSqlSupport;
import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.mapper.WvpProxyDeviceMapper; import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.mapper.WvpProxyDeviceMapper;
import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.model.WvpProxyDevice; import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.model.WvpProxyDevice;
@ -8,6 +9,7 @@ import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -43,25 +45,26 @@ public class DeviceService {
* @param device 设备 * @param device 设备
* @return 是否成功 * @return 是否成功
*/ */
public boolean addDevice(WvpProxyDevice device){ @SneakyThrows
public boolean addDevice(WvpProxyDevice device) {
if(device == null){ if(device == null){
return false; return false;
} }
String deviceCode = device.getDeviceCode(); String deviceCode = device.getDeviceCode();
if(StringUtils.isBlank(deviceCode)){ if(StringUtils.isBlank(deviceCode)){
throw new RuntimeException("设备编码不能为空"); throw new JsonException("设备编码不能为空");
} }
if(getDeviceByDeviceCode(deviceCode).isPresent()){ if(getDeviceByDeviceCode(deviceCode).isPresent()){
throw new RuntimeException(MessageFormat.format("设备编码 {0} 已存在" ,deviceCode)); throw new JsonException(MessageFormat.format("设备编码 {0} 已存在" ,deviceCode));
} }
String gbDeviceId = device.getGbDeviceId(); String gbDeviceId = device.getGbDeviceId();
if(StringUtils.isBlank(gbDeviceId)){ if(StringUtils.isBlank(gbDeviceId)){
throw new RuntimeException("国标编码不能为空"); throw new JsonException("国标编码不能为空");
} }
if(getDeviceByGbDeviceId(gbDeviceId).isPresent()){ if(getDeviceByGbDeviceId(gbDeviceId).isPresent()){
throw new RuntimeException(MessageFormat.format("国标编码 {0} 已存在" ,gbDeviceId)); throw new JsonException(MessageFormat.format("国标编码 {0} 已存在" ,gbDeviceId));
} }
return deviceMapper.insert(device) > 0; return deviceMapper.insert(device) > 0;