调整参数校验规则

This commit is contained in:
akun 2024-05-06 16:19:35 +08:00
parent c4d902d288
commit 9d959de9cc
7 changed files with 87 additions and 34 deletions

View File

@ -6,6 +6,7 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@ -24,10 +25,12 @@ import java.util.List;
public class InsertGatewayInterfaceInfoDTO {
private static final long serialVersionUID = 1L;
@Length(max = 50,message = "接口名称不能超过50个字符")
@NotBlank(message = "接口名称不能为空")
@ApiModelProperty("接口名称")
private String interfaceName;
@Length(max = 200,message = "接口名称不能超过200个字符")
@NotBlank(message = "接口路径不能为空")
@ApiModelProperty("接口路径")
private String interfacePath;
@ -36,15 +39,14 @@ public class InsertGatewayInterfaceInfoDTO {
@ApiModelProperty("请求方式")
private String requestMethod;
@Length(max = 200,message = "接口描述不能超过200个字符")
@ApiModelProperty("接口描述")
private String description;
@Length(max = 20,message = "接口版本不能超过20个字符")
@ApiModelProperty("接口版本")
private String version;
@ApiModelProperty("接口状态(0=停用,1=启用)")
private String status;
@ApiModelProperty("接口文档(文档地址)")
private String document;

View File

@ -4,6 +4,7 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@ -26,10 +27,12 @@ public class UpdateGatewayInterfaceInfoDTO {
@ApiModelProperty("ID")
private Long id;
@Length(max = 50,message = "接口名称不能超过50个字符")
@NotBlank(message = "接口名称不能为空")
@ApiModelProperty("接口名称")
private String interfaceName;
@Length(max = 200,message = "接口名称不能超过200个字符")
@NotBlank(message = "接口路径不能为空")
@ApiModelProperty("接口路径")
private String interfacePath;
@ -41,12 +44,11 @@ public class UpdateGatewayInterfaceInfoDTO {
@ApiModelProperty("接口描述")
private String description;
@Length(max = 20,message = "接口版本不能超过20个字符")
@NotBlank(message = "接口版本不能为空")
@ApiModelProperty("接口版本")
private String version;
@ApiModelProperty("接口状态(0=停用,1=启用)")
private String status;
@ApiModelProperty("接口文档(文档地址)")
private String document;

View File

@ -65,4 +65,6 @@ public interface GatewayInterfaceInfoMapper
int updateGatewayInterfaceInfoStatusByIds(@Param("ids") Collection<Long> ids, @Param("status") String status);
List<GatewayInterfaceInfo> selectGatewayInterfaceInfoByServerIds(@Param("serverIds") Collection<Long> serverIds);
List<GatewayInterfaceInfo> selectGatewayInterfaceInfoByIds(@Param("ids") Collection<Long> ids);
}

View File

@ -8,7 +8,6 @@ import com.sf.service.gateway.enums.GatewayDataStatus;
import java.util.Collection;
import java.util.List;
import java.util.Set;
/**
* 接口管理Service接口
@ -70,6 +69,8 @@ public interface IGatewayInterfaceInfoService
List<GatewayInterfaceInfo> selectGatewayInterfaceInfoByServerIds(Collection<Long> serverIds);
List<GatewayInterfaceInfo> selectGatewayInterfaceInfoByIds(Collection<Long> ids);
/**
* 添加接口信息及绑定的策略信息
*

View File

@ -25,6 +25,8 @@ import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
/**
* 接口管理Service业务层处理
*
@ -35,13 +37,13 @@ import org.springframework.util.CollectionUtils;
@Service
public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoService {
@Autowired
@Resource
private GatewayInterfaceInfoMapper gatewayInterfaceInfoMapper;
@Autowired
@Resource
private GatewayInterfaceLinkStrategyMapper gatewayInterfaceLinkStrategyMapper;
@Autowired
@Resource
private IGatewayStrategyService gatewayStrategyService;
/**
@ -74,24 +76,37 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer
*/
@Override
public Long insertGatewayInterfaceInfo(GatewayInterfaceInfo gatewayInterfaceInfo) {
gatewayInterfaceInfo.setAppCode(AppUtils.getAppCodeFromRequestHeader());
String appCode = AppUtils.getAppCodeFromRequestHeader();
gatewayInterfaceInfo.setAppCode(appCode);
String username = SecurityUtils.getUsername();
gatewayInterfaceInfo.setCreated(username);
gatewayInterfaceInfo.setModified(username);
Date nowDate = DateUtils.getNowDate();
gatewayInterfaceInfo.setCreateTime(nowDate);
gatewayInterfaceInfo.setUpdateTime(nowDate);
Assert.hasText(gatewayInterfaceInfo.getInterfacePath(),"接口路径不能为空");
Assert.hasText(gatewayInterfaceInfo.getInterfacePath(), "接口路径不能为空");
gatewayInterfaceInfo.setUriRegular(RegexUtils.isRegex(gatewayInterfaceInfo.getInterfacePath()));
try {
gatewayInterfaceInfoMapper.insertGatewayInterfaceInfo(gatewayInterfaceInfo);
return gatewayInterfaceInfo.getId();
}catch (DuplicateKeyException e){
throw new IllegalArgumentException("接口名称不可重复!");
} catch (DuplicateKeyException e) {
throw new IllegalArgumentException("同名接口,版本不可重复!");
}
}
private void checkGatewayInterfaceInfoCanEnable(GatewayInterfaceInfo current) {
GatewayInterfaceInfo query = new GatewayInterfaceInfo();
query.setStatus(GatewayDataStatus.ENABLE.getCode());
query.setAppCode(current.getAppCode());
query.setInterfacePath(current.getInterfacePath());
List<GatewayInterfaceInfo> infoList = gatewayInterfaceInfoMapper.selectGatewayInterfaceInfoList(query);
if (CollUtil.isNotEmpty(infoList)) {
Assert.isTrue(infoList.size() == 1,"该请求路径已存在启用的记录");
Assert.isTrue(infoList.get(0).getId().equals(current.getId()),"该请求路径已存在启用的记录");
}
}
/**
* 修改接口管理
*
@ -103,12 +118,12 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer
gatewayInterfaceInfo.setModified(SecurityUtils.getUsername());
gatewayInterfaceInfo.setUpdateTime(DateUtils.getNowDate());
String interfacePath = gatewayInterfaceInfo.getInterfacePath();
Assert.hasText(interfacePath,"接口路径不能为空");
Assert.hasText(interfacePath, "接口路径不能为空");
gatewayInterfaceInfo.setUriRegular(RegexUtils.isRegex(interfacePath));
try {
return gatewayInterfaceInfoMapper.updateGatewayInterfaceInfo(gatewayInterfaceInfo);
}catch (DuplicateKeyException e){
throw new IllegalArgumentException("接口名称不可重复!");
} catch (DuplicateKeyException e) {
throw new IllegalArgumentException("同名接口,版本不可重复!");
}
}
@ -146,21 +161,33 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer
@Override
public int updateGatewayInterfaceInfoStatusByIds(Collection<Long> ids, GatewayDataStatus status) {
if (CollectionUtils.isEmpty(ids) || status == null){
if (CollectionUtils.isEmpty(ids) || status == null) {
log.warn("Insufficient update conditions");
return 0;
}
return gatewayInterfaceInfoMapper.updateGatewayInterfaceInfoStatusByIds(ids,status.getCode());
if (GatewayDataStatus.ENABLE.equals(status)) {
List<GatewayInterfaceInfo> infoList = selectGatewayInterfaceInfoByIds(ids);
infoList.forEach(this::checkGatewayInterfaceInfoCanEnable);
}
return gatewayInterfaceInfoMapper.updateGatewayInterfaceInfoStatusByIds(ids, status.getCode());
}
@Override
public List<GatewayInterfaceInfo> selectGatewayInterfaceInfoByServerIds(Collection<Long> serverIds) {
if (CollectionUtils.isEmpty(serverIds)){
if (CollectionUtils.isEmpty(serverIds)) {
return Collections.emptyList();
}
return gatewayInterfaceInfoMapper.selectGatewayInterfaceInfoByServerIds(serverIds);
}
@Override
public List<GatewayInterfaceInfo> selectGatewayInterfaceInfoByIds(Collection<Long> ids) {
if (CollectionUtils.isEmpty(ids)) {
return Collections.emptyList();
}
return gatewayInterfaceInfoMapper.selectGatewayInterfaceInfoByIds(ids);
}
@Transactional(rollbackFor = Exception.class)
@Override
public Long insertGatewayInterfaceInfoAndBindStrategy(InsertGatewayInterfaceInfoDTO dto) {
@ -170,11 +197,11 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer
gatewayInterfaceInfo.setRequestMethod(dto.getRequestMethod());
gatewayInterfaceInfo.setDescription(dto.getDescription());
gatewayInterfaceInfo.setVersion(dto.getVersion());
gatewayInterfaceInfo.setStatus(dto.getStatus());
gatewayInterfaceInfo.setStatus(GatewayDataStatus.DISABLE.getCode());
gatewayInterfaceInfo.setDocument(dto.getDocument());
gatewayInterfaceInfo.setServerId(dto.getServerId());
Long interfaceInfoId = insertGatewayInterfaceInfo(gatewayInterfaceInfo);
if (CollUtil.isNotEmpty(dto.getStrategyIds())){
if (CollUtil.isNotEmpty(dto.getStrategyIds())) {
checkGatewayStrategyTypeCannotBeRepeated(dto.getStrategyIds());
List<GatewayInterfaceLinkStrategy> linkStrategyList = dto.getStrategyIds()
.stream().map(strategyId -> new GatewayInterfaceLinkStrategy(interfaceInfoId, strategyId))
@ -194,13 +221,12 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer
gatewayInterfaceInfo.setRequestMethod(dto.getRequestMethod());
gatewayInterfaceInfo.setDescription(dto.getDescription());
gatewayInterfaceInfo.setVersion(dto.getVersion());
gatewayInterfaceInfo.setStatus(dto.getStatus());
gatewayInterfaceInfo.setDocument(dto.getDocument());
gatewayInterfaceInfo.setServerId(dto.getServerId());
updateGatewayInterfaceInfo(gatewayInterfaceInfo);
// 先删除
gatewayInterfaceLinkStrategyMapper.deleteByInterfaceIds(Collections.singleton(dto.getId()));
if (CollUtil.isNotEmpty(dto.getStrategyIds())){
if (CollUtil.isNotEmpty(dto.getStrategyIds())) {
checkGatewayStrategyTypeCannotBeRepeated(dto.getStrategyIds());
List<GatewayInterfaceLinkStrategy> linkStrategyList = dto.getStrategyIds()
.stream().map(strategyId -> new GatewayInterfaceLinkStrategy(dto.getId(), strategyId))
@ -212,7 +238,7 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer
@Override
public List<GatewayInterfaceLinkStrategy> selectGatewayInterfaceLinkStrategyByInterfaceIds(Collection<Long> interfaceIds) {
if (CollectionUtils.isEmpty(interfaceIds)){
if (CollectionUtils.isEmpty(interfaceIds)) {
return Collections.emptyList();
}
return gatewayInterfaceLinkStrategyMapper.selectGatewayInterfaceLinkStrategyByInterfaceIds(interfaceIds);
@ -221,7 +247,7 @@ public class GatewayInterfaceInfoServiceImpl implements IGatewayInterfaceInfoSer
private void checkGatewayStrategyTypeCannotBeRepeated(List<Long> strategyIds) {
List<GatewayStrategy> strategyList = gatewayStrategyService.selectGatewayStrategyByIds(strategyIds);
Map<String, List<GatewayStrategy>> groupByStrategyTypeMap = strategyList.stream().collect(Collectors.groupingBy(GatewayStrategy::getStrategyType));
groupByStrategyTypeMap.forEach((type,strategies) ->{
groupByStrategyTypeMap.forEach((type, strategies) -> {
Assert.isTrue(strategies.size() == 1, Objects.requireNonNull(GatewayStrategyType.getByCode(type)).getInfo() + "类型只能选择一个!");
});
}

View File

@ -24,6 +24,8 @@ import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import javax.annotation.Resource;
/**
* 策略管理Service业务层处理
*
@ -34,10 +36,10 @@ import org.springframework.util.StringUtils;
@Service
public class GatewayStrategyServiceImpl implements IGatewayStrategyService
{
@Autowired
@Resource
private GatewayStrategyMapper gatewayStrategyMapper;
@Autowired
@Resource
private GatewayInterfaceLinkStrategyMapper gatewayInterfaceLinkStrategyMapper;
/**
@ -76,6 +78,17 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService
Date nowDate = DateUtils.getNowDate();
gatewayStrategy.setCreateTime(nowDate);
gatewayStrategy.setUpdateTime(nowDate);
checkStrategyConfig(gatewayStrategy);
try {
return gatewayStrategyMapper.insertGatewayStrategy(gatewayStrategy);
}catch (DuplicateKeyException e){
throw new IllegalArgumentException("策略名称不可重复!");
}
}
private void checkStrategyConfig(GatewayStrategy gatewayStrategy) {
GatewayStrategyType strategyType = GatewayStrategyType.getByCode(gatewayStrategy.getStrategyType());
Assert.notNull(strategyType,"不支持的策略类型!");
switch (strategyType){
@ -94,12 +107,6 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService
default:
break;
}
try {
return gatewayStrategyMapper.insertGatewayStrategy(gatewayStrategy);
}catch (DuplicateKeyException e){
throw new IllegalArgumentException("策略名称不可重复!");
}
}
/**
@ -166,6 +173,11 @@ public class GatewayStrategyServiceImpl implements IGatewayStrategyService
@Override
public int updateGatewayStrategy(GatewayStrategy gatewayStrategy)
{
// 策略类型不支持调整
GatewayStrategy inDbStrategy = selectGatewayStrategyById(gatewayStrategy.getId());
Assert.notNull(inDbStrategy,"数据不存在");
gatewayStrategy.setStrategyType(inDbStrategy.getStrategyType());
checkStrategyConfig(gatewayStrategy);
gatewayStrategy.setUpdateTime(DateUtils.getNowDate());
try {
return gatewayStrategyMapper.updateGatewayStrategy(gatewayStrategy);

View File

@ -35,6 +35,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="interfaceName != null and interfaceName != ''"> and interface_name like concat('%', #{interfaceName}, '%')</if>
<if test="status != null and status != ''"> and status = #{status}</if>
<if test="appCode != null and appCode != ''"> and app_code = #{appCode}</if>
<if test="interfacePath != null and interfacePath != ''"> and interface_path = #{interfacePath}</if>
<if test="params.beginCreateTime != null and params.beginCreateTime != '' and params.endCreateTime != null and params.endCreateTime != ''"> and create_time between #{params.beginCreateTime} and #{params.endCreateTime}</if>
</where>
</select>
@ -50,6 +51,13 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
#{serverId}
</foreach>
</select>
<select id="selectGatewayInterfaceInfoByIds" resultMap="GatewayInterfaceInfoResult">
<include refid="selectGatewayInterfaceInfoVo"/>
where id in
<foreach item="id" collection="ids" open="(" separator="," close=")">
#{id}
</foreach>
</select>
<insert id="insertGatewayInterfaceInfo" parameterType="GatewayInterfaceInfo" useGeneratedKeys="true" keyProperty="id">
insert into Gateway_interface_info