refactor(exception): 优化异常处理并统一异常类型

- 移除 ApiExceptionHandler 中的重复异常处理方法
- 在 PropertyServiceImpl 中使用自定义 ApiException 替代 RuntimeException- 删除 DeviceService 中未使用的导入
- 移除 SpringSecurityConfig 中的 @EnableWebSecurity 注解
This commit is contained in:
zhuangtianxiang 2025-01-08 10:17:44 +08:00
parent bd6f4c2ed6
commit 1e4fb52905
4 changed files with 9 additions and 13 deletions

View File

@ -65,11 +65,10 @@ public class ApiExceptionHandler {
return new ResponseEntity<>(objectMapper.writeValueAsString(Map.of("msg", ex.getMessage())), HttpStatus.INTERNAL_SERVER_ERROR); return new ResponseEntity<>(objectMapper.writeValueAsString(Map.of("msg", ex.getMessage())), HttpStatus.INTERNAL_SERVER_ERROR);
} }
//TODO 冲突 // @ExceptionHandler(value = {Exception.class})
@ExceptionHandler(value = {Exception.class}) // public ResponseEntity<Object> handleException(Exception ex) throws JsonProcessingException {
public ResponseEntity<Object> handleException(Exception ex) throws JsonProcessingException { // log.error("Exception: {}", objectMapper.writeValueAsString(Map.of("msg", ex.getMessage())));
log.error("Exception: {}", objectMapper.writeValueAsString(Map.of("msg", ex.getMessage()))); // return new ResponseEntity<>(objectMapper.writeValueAsString(Map.of("msg", ex.getMessage())), HttpStatus.INTERNAL_SERVER_ERROR);
return new ResponseEntity<>(objectMapper.writeValueAsString(Map.of("msg", ex.getMessage())), HttpStatus.INTERNAL_SERVER_ERROR); // }
}
} }

View File

@ -33,7 +33,6 @@ import javax.sql.DataSource;
@AllArgsConstructor @AllArgsConstructor
@EnableMethodSecurity @EnableMethodSecurity
@Configuration @Configuration
@EnableWebSecurity
public class SpringSecurityConfig { public class SpringSecurityConfig {
private final UserDetailsService userDetailsService; private final UserDetailsService userDetailsService;

View File

@ -1,14 +1,11 @@
package com.zsc.edu.gateway.modules.iot.device.service; package com.zsc.edu.gateway.modules.iot.device.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import com.zsc.edu.gateway.modules.iot.device.dto.BatchDeviceDto; import com.zsc.edu.gateway.modules.iot.device.dto.BatchDeviceDto;
import com.zsc.edu.gateway.modules.iot.device.dto.DeviceDto; import com.zsc.edu.gateway.modules.iot.device.dto.DeviceDto;
import com.zsc.edu.gateway.modules.iot.device.dto.DeviceServeDto; import com.zsc.edu.gateway.modules.iot.device.dto.DeviceServeDto;
import com.zsc.edu.gateway.modules.iot.device.entity.Device; import com.zsc.edu.gateway.modules.iot.device.entity.Device;
import com.zsc.edu.gateway.modules.iot.device.entity.DeviceDiff; import com.zsc.edu.gateway.modules.iot.device.entity.DeviceDiff;
import com.zsc.edu.gateway.modules.iot.device.query.DeviceQuery;
import com.zsc.edu.gateway.modules.iot.device.vo.DeviceVo; import com.zsc.edu.gateway.modules.iot.device.vo.DeviceVo;
import java.util.List; import java.util.List;

View File

@ -1,6 +1,7 @@
package com.zsc.edu.gateway.modules.iot.tsl.service.impl; package com.zsc.edu.gateway.modules.iot.tsl.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsc.edu.gateway.exception.ApiException;
import com.zsc.edu.gateway.modules.iot.tsl.dto.PropertyDto; import com.zsc.edu.gateway.modules.iot.tsl.dto.PropertyDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Property; import com.zsc.edu.gateway.modules.iot.tsl.entity.Property;
import com.zsc.edu.gateway.modules.iot.tsl.mapper.PropertyMapper; import com.zsc.edu.gateway.modules.iot.tsl.mapper.PropertyMapper;
@ -25,7 +26,7 @@ public class PropertyServiceImpl extends ServiceImpl<PropertyRepository, Propert
@Override @Override
public Property create(PropertyDto dto) { public Property create(PropertyDto dto) {
if (baseMapper.findByName(dto.getName()) != null) { if (baseMapper.findByName(dto.getName()) != null) {
throw new RuntimeException("该属性已存在!"); throw new ApiException("该属性已存在!");
} }
Property property = mapper.toEntity(dto); Property property = mapper.toEntity(dto);
save(property); save(property);
@ -40,7 +41,7 @@ public class PropertyServiceImpl extends ServiceImpl<PropertyRepository, Propert
public Property update(PropertyDto dto, Long id) { public Property update(PropertyDto dto, Long id) {
Property property = baseMapper.selectById(id); Property property = baseMapper.selectById(id);
if (property == null) { if (property == null) {
throw new RuntimeException("Serve not found with id: " + id); throw new ApiException("Serve not found with id: " + id);
} }
mapper.convert(dto, property); mapper.convert(dto, property);
baseMapper.updateById(property); baseMapper.updateById(property);
@ -51,7 +52,7 @@ public class PropertyServiceImpl extends ServiceImpl<PropertyRepository, Propert
public Property detail(Long id) { public Property detail(Long id) {
Property property = baseMapper.selectById(id); Property property = baseMapper.selectById(id);
if (property == null) { if (property == null) {
throw new RuntimeException("属性不存在请检查输入ID是否正确"); throw new ApiException("属性不存在请检查输入ID是否正确");
} }
return property; return property;
} }