Vo参数校验,SysNotificationMapper.xml代码完善
This commit is contained in:
parent
e690967207
commit
1ebf69fac2
|
|
@ -0,0 +1,24 @@
|
|||
package cn.org.gitlink.notification.common.utils;
|
||||
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @description: Validator工具类
|
||||
* @author: wanjia
|
||||
* @time: 2021/9/9
|
||||
*/
|
||||
public class ValidatorUtils {
|
||||
public static Map<String, String> buildValidationErrorMessageMap(BindingResult bindingResult) {
|
||||
Map<String, String> errorMessageMap = new HashMap<>();
|
||||
List<FieldError> errList = bindingResult.getFieldErrors();
|
||||
for (FieldError error : errList) {
|
||||
errorMessageMap.put(error.getField(), error.getDefaultMessage());
|
||||
}
|
||||
return errorMessageMap;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,33 +1,40 @@
|
|||
package cn.org.gitlink.notification.model.dao.entity.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import org.hibernate.validator.constraints.Range;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Pattern;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
public class NewSysNotificationVo {
|
||||
|
||||
@ApiModelProperty(value = "平台编码", required = true)
|
||||
@NotBlank
|
||||
@NotBlank(message = "平台编码不能为空")
|
||||
private String platform;
|
||||
|
||||
@ApiModelProperty(value = "消息发送者", required = true)
|
||||
@NotNull
|
||||
private long sender;
|
||||
@NotNull(message = "消息发送者不能为空")
|
||||
@Range(min = Long.MIN_VALUE, max = Long.MAX_VALUE)
|
||||
private Long sender;
|
||||
|
||||
@ApiModelProperty(value = "消息接受者", required = true)
|
||||
@NotBlank
|
||||
@ApiModelProperty(value = "消息接收者", required = true)
|
||||
@Pattern(regexp = "^\\d+(,\\d+)*$", message = "接收者id串非法")
|
||||
@NotBlank(message = "消息接收者不能为空")
|
||||
private String receivers;
|
||||
|
||||
@ApiModelProperty(value = "消息内容", required = true)
|
||||
@NotBlank
|
||||
@NotBlank(message = "消息内容不能为空")
|
||||
private String content;
|
||||
|
||||
@ApiModelProperty(value = "消息跳转链接")
|
||||
@Size(max = 2000, message = "消息跳转链接长度在0~2000")
|
||||
private String notification_url;
|
||||
|
||||
@ApiModelProperty(value = "消息类型 1-系统消息 2-@我", required = true)
|
||||
@NotNull
|
||||
@Range(min = 1, max = 2)
|
||||
@NotNull(message = "消息类型不能为空")
|
||||
private Integer type;
|
||||
|
||||
@ApiModelProperty(value = "消息来源")
|
||||
|
|
@ -60,11 +67,11 @@ public class NewSysNotificationVo {
|
|||
this.platform = platform;
|
||||
}
|
||||
|
||||
public long getSender() {
|
||||
public Long getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(long sender) {
|
||||
public void setSender(Long sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,9 +1,11 @@
|
|||
package cn.org.gitlink.notification.model.dao.entity.vo;
|
||||
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import org.hibernate.validator.constraints.Range;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Pattern;
|
||||
|
||||
public class UpdateNotificationStatusVo {
|
||||
@ApiModelProperty(value = "平台编号", required = true)
|
||||
|
|
@ -12,10 +14,12 @@ public class UpdateNotificationStatusVo {
|
|||
|
||||
@ApiModelProperty(value = "消息ids", required = true)
|
||||
@NotBlank
|
||||
@Pattern(regexp = "^\\d+(,\\d+)*$", message = "消息id串非法")
|
||||
private String notificationIds;
|
||||
|
||||
@ApiModelProperty(value = "已读状态: 1未读,2已读", required = true)
|
||||
@NotNull
|
||||
@Range(min = 1, max = 2)
|
||||
private Integer status;
|
||||
|
||||
public String getPlatform() {
|
||||
|
|
|
|||
|
|
@ -11,9 +11,11 @@
|
|||
<result column="status" property="status" jdbcType="TINYINT"/>
|
||||
<result column="type" property="type" jdbcType="TINYINT"/>
|
||||
<result column="is_delete" property="isDelete" jdbcType="BIT"/>
|
||||
<result column="source" property="content" jdbcType="VARCHAR"/>
|
||||
<result column="extra" property="extra" jdbcType="VARCHAR"/>
|
||||
</resultMap>
|
||||
<sql id="Base_Column_List">
|
||||
id, sender, receiver, content, notification_url, created_at, status, type, is_delete, status, extra
|
||||
id, sender, receiver, content, notification_url, created_at, status, type, is_delete, source, extra
|
||||
</sql>
|
||||
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer">
|
||||
select
|
||||
|
|
@ -28,7 +30,7 @@
|
|||
<insert id="insert" parameterType="cn.org.gitlink.notification.model.dao.entity.SysNotification">
|
||||
insert into ${platform}_sys_notification (id, sender, receiver,
|
||||
content, notification_url,
|
||||
status, type)
|
||||
status, type, source, extra)
|
||||
values (#{record.id,jdbcType=INTEGER}, #{record.sender,jdbcType=INTEGER}, #{record.receiver,jdbcType=INTEGER},
|
||||
#{record.content,jdbcType=VARCHAR}, #{record.notificationUrl,jdbcType=VARCHAR},
|
||||
#{record.status,jdbcType=TINYINT}, #{record.type,jdbcType=TINYINT})
|
||||
|
|
@ -64,6 +66,12 @@
|
|||
<if test="record.isDelete != null">
|
||||
is_delete,
|
||||
</if>
|
||||
<if test="record.source != null">
|
||||
source,
|
||||
</if>
|
||||
<if test="record.extra != null">
|
||||
extra,
|
||||
</if>
|
||||
</trim>
|
||||
<trim prefix="values (" suffix=")" suffixOverrides=",">
|
||||
<if test="record.id != null">
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@ package cn.org.gitlink.notification.writer.controller;
|
|||
import cn.org.gitlink.notification.common.response.DataPacketUtil;
|
||||
import cn.org.gitlink.notification.common.response.ResponseData;
|
||||
import cn.org.gitlink.notification.common.utils.KafkaUtil;
|
||||
import cn.org.gitlink.notification.common.utils.ValidatorUtils;
|
||||
import cn.org.gitlink.notification.model.dao.entity.vo.NewSysNotificationVo;
|
||||
import cn.org.gitlink.notification.model.service.notification.SysNotificationService;
|
||||
import cn.org.gitlink.notification.model.dao.entity.vo.UpdateNotificationStatusVo;
|
||||
|
|
@ -11,13 +12,19 @@ import io.swagger.annotations.ApiOperation;
|
|||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(value = "/gns/notification")
|
||||
public class NotificationController {
|
||||
|
||||
//平台编号
|
||||
private static final String PLATFORM_CODE_GITLINK = "gitlink";
|
||||
|
||||
@Autowired
|
||||
private KafkaUtil kafkaUtil;
|
||||
|
||||
|
|
@ -29,15 +36,36 @@ public class NotificationController {
|
|||
@ApiOperation("添加系统消息")
|
||||
@RequestMapping(path = "/", method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
public ResponseData sendNotification(@Validated @RequestBody NewSysNotificationVo newSysNotificationVo) {
|
||||
kafkaUtil.sendMessage("gitlink_notification", JSONObject.toJSONString(newSysNotificationVo));
|
||||
return DataPacketUtil.jsonSuccessResult();
|
||||
public ResponseData sendNotification(@Validated @RequestBody NewSysNotificationVo newSysNotificationVo, BindingResult bindingResult) {
|
||||
//参数合法性验证
|
||||
Map<String, String> errors = ValidatorUtils.buildValidationErrorMessageMap(bindingResult);
|
||||
if (!errors.isEmpty()) {
|
||||
return DataPacketUtil.jsonFailResult(errors);
|
||||
}
|
||||
ResponseData jsonFailResult = validatePlatformCode(newSysNotificationVo.getPlatform());
|
||||
if (jsonFailResult != null) return jsonFailResult;
|
||||
|
||||
try {
|
||||
kafkaUtil.sendMessage("gitlink_notification", JSONObject.toJSONString(newSysNotificationVo));
|
||||
return DataPacketUtil.jsonSuccessResult();
|
||||
} catch (Exception e) {
|
||||
logger.error(e);
|
||||
return DataPacketUtil.jsonFailResult(e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ApiOperation("改变系统消息状态")
|
||||
@RequestMapping(path = "/", method = RequestMethod.PUT)
|
||||
@ResponseBody
|
||||
public ResponseData changeNotificationStatus(@Validated @RequestBody UpdateNotificationStatusVo updateNotificationStatusVo) {
|
||||
public ResponseData changeNotificationStatus(@Validated @RequestBody UpdateNotificationStatusVo updateNotificationStatusVo, BindingResult bindingResult) {
|
||||
//参数合法性验证
|
||||
Map<String, String> errors = ValidatorUtils.buildValidationErrorMessageMap(bindingResult);
|
||||
if (!errors.isEmpty()) {
|
||||
return DataPacketUtil.jsonFailResult(errors);
|
||||
}
|
||||
ResponseData jsonFailResult = validatePlatformCode(updateNotificationStatusVo.getPlatform());
|
||||
if (jsonFailResult != null) return jsonFailResult;
|
||||
|
||||
try {
|
||||
sysNotificationService.markNotificationAs(updateNotificationStatusVo.getPlatform(), updateNotificationStatusVo.getNotificationIds(), updateNotificationStatusVo.getStatus());
|
||||
|
|
@ -47,4 +75,21 @@ public class NotificationController {
|
|||
return DataPacketUtil.jsonFailResult(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 验证 platform 和 receiver 的合法性
|
||||
*
|
||||
* @param platform
|
||||
* @return
|
||||
*/
|
||||
private ResponseData validatePlatformCode(String platform) {
|
||||
|
||||
//验证 {platform} 参数合法性,以判断请求来源
|
||||
if (!platform.trim().toLowerCase().equals(PLATFORM_CODE_GITLINK)) {
|
||||
logger.debug("\t 输入参数 {platform} 的值 {" + platform + "} 无效");
|
||||
return DataPacketUtil.jsonFailResult("{platform} 参数非法");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue