refactor(exception): 优化异常处理并统一异常类型
- 移除 ApiExceptionHandler 中的重复异常处理方法 - 在 PropertyServiceImpl 中使用自定义 ApiException 替代 RuntimeException- 删除 DeviceService 中未使用的导入 - 移除 SpringSecurityConfig 中的 @EnableWebSecurity 注解
This commit is contained in:
parent
bd6f4c2ed6
commit
1e4fb52905
@ -65,11 +65,10 @@ public class ApiExceptionHandler {
|
||||
return new ResponseEntity<>(objectMapper.writeValueAsString(Map.of("msg", ex.getMessage())), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
|
||||
//TODO 冲突
|
||||
@ExceptionHandler(value = {Exception.class})
|
||||
public ResponseEntity<Object> handleException(Exception ex) throws JsonProcessingException {
|
||||
log.error("Exception: {}", objectMapper.writeValueAsString(Map.of("msg", ex.getMessage())));
|
||||
return new ResponseEntity<>(objectMapper.writeValueAsString(Map.of("msg", ex.getMessage())), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
// @ExceptionHandler(value = {Exception.class})
|
||||
// public ResponseEntity<Object> handleException(Exception ex) throws JsonProcessingException {
|
||||
// log.error("Exception: {}", objectMapper.writeValueAsString(Map.of("msg", ex.getMessage())));
|
||||
// return new ResponseEntity<>(objectMapper.writeValueAsString(Map.of("msg", ex.getMessage())), HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
// }
|
||||
|
||||
}
|
||||
|
@ -33,7 +33,6 @@ import javax.sql.DataSource;
|
||||
@AllArgsConstructor
|
||||
@EnableMethodSecurity
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SpringSecurityConfig {
|
||||
|
||||
private final UserDetailsService userDetailsService;
|
||||
|
@ -1,14 +1,11 @@
|
||||
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.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.DeviceServeDto;
|
||||
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.query.DeviceQuery;
|
||||
import com.zsc.edu.gateway.modules.iot.device.vo.DeviceVo;
|
||||
|
||||
import java.util.List;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.zsc.edu.gateway.modules.iot.tsl.service.impl;
|
||||
|
||||
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.entity.Property;
|
||||
import com.zsc.edu.gateway.modules.iot.tsl.mapper.PropertyMapper;
|
||||
@ -25,7 +26,7 @@ public class PropertyServiceImpl extends ServiceImpl<PropertyRepository, Propert
|
||||
@Override
|
||||
public Property create(PropertyDto dto) {
|
||||
if (baseMapper.findByName(dto.getName()) != null) {
|
||||
throw new RuntimeException("该属性已存在!");
|
||||
throw new ApiException("该属性已存在!");
|
||||
}
|
||||
Property property = mapper.toEntity(dto);
|
||||
save(property);
|
||||
@ -40,7 +41,7 @@ public class PropertyServiceImpl extends ServiceImpl<PropertyRepository, Propert
|
||||
public Property update(PropertyDto dto, Long id) {
|
||||
Property property = baseMapper.selectById(id);
|
||||
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);
|
||||
baseMapper.updateById(property);
|
||||
@ -51,7 +52,7 @@ public class PropertyServiceImpl extends ServiceImpl<PropertyRepository, Propert
|
||||
public Property detail(Long id) {
|
||||
Property property = baseMapper.selectById(id);
|
||||
if (property == null) {
|
||||
throw new RuntimeException("属性不存在!请检查输入ID是否正确!");
|
||||
throw new ApiException("属性不存在!请检查输入ID是否正确!");
|
||||
}
|
||||
return property;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user