valid校验异常全局处理
This commit is contained in:
parent
d0c1400768
commit
400e39a320
|
|
@ -7,11 +7,16 @@ import cn.org.gitlink.notification.common.utils.CommonUtils;
|
|||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* web请求异常全局处理类,在使用的模块内继承此类,然后加上ControllerAdvice注解,example:
|
||||
|
|
@ -39,4 +44,24 @@ public class ExceptionInterceptor {
|
|||
logger.error(logTemplate, request.getRequestURI(), e);
|
||||
return DataPacketUtil.jsonFailResult(e.getMessage());
|
||||
}
|
||||
|
||||
/**
|
||||
* valid 异常处理
|
||||
*
|
||||
* @param e
|
||||
* @return
|
||||
*/
|
||||
@ResponseBody
|
||||
@ResponseStatus(HttpStatus.OK)
|
||||
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||
public ResponseData handlerValidException(MethodArgumentNotValidException e){
|
||||
Map<String, Object> errorMessageMap = new HashMap<>();
|
||||
// 获取所有错误信息
|
||||
List<FieldError> errList = e.getBindingResult().getFieldErrors();
|
||||
for (FieldError error : errList) {
|
||||
errorMessageMap.put(error.getField(), error.getDefaultMessage());
|
||||
}
|
||||
logger.info("data errors:{}", errorMessageMap);
|
||||
return DataPacketUtil.jsonFailResult(errorMessageMap);
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,6 @@ import cn.org.gitlink.notification.common.utils.KafkaUtil;
|
|||
import cn.org.gitlink.notification.model.dao.entity.vo.NewEmailJobVo;
|
||||
import cn.org.gitlink.notification.model.service.notification.EmailJobsService;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import org.apache.kafka.clients.admin.NewTopic;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
@ -15,8 +14,6 @@ import org.springframework.kafka.annotation.KafkaHandler;
|
|||
import org.springframework.kafka.annotation.KafkaListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@Component
|
||||
@Configuration
|
||||
@KafkaListener(topics = "${spring.kafka.consumer.topic_email}", groupId = "${spring.kafka.consumer.group_id_email}")
|
||||
|
|
|
|||
|
|
@ -41,14 +41,8 @@ public class EmailJobsController {
|
|||
public ResponseData sendEmail(@ApiParam(value = "平台编码", required = true)
|
||||
@PathVariable(name = "platform") String platform,
|
||||
|
||||
@Validated @RequestBody NewEmailJobParamsVo newEmailJobParamsVo,
|
||||
|
||||
BindingResult bindingResult){
|
||||
@Validated @RequestBody NewEmailJobParamsVo newEmailJobParamsVo){
|
||||
//参数合法性验证
|
||||
Map<String, String> errors = ValidatorUtils.buildValidationErrorMessageMap(bindingResult);
|
||||
if (!errors.isEmpty()) {
|
||||
return DataPacketUtil.jsonFailResult(errors);
|
||||
}
|
||||
ResponseData jsonFailResult = ValidatorUtils.validatePlatformCode(platform);
|
||||
if (jsonFailResult != null) return jsonFailResult;
|
||||
|
||||
|
|
|
|||
|
|
@ -48,15 +48,9 @@ public class NotificationController {
|
|||
public ResponseData sendNotification(@ApiParam(value = "平台编码", required = true)
|
||||
@PathVariable(name = "platform") String platform,
|
||||
|
||||
@Validated @RequestBody NewSysNotificationParamsVo newSysNotificationParamsVo,
|
||||
@Validated @RequestBody NewSysNotificationParamsVo newSysNotificationParamsVo) {
|
||||
|
||||
BindingResult bindingResult) {
|
||||
|
||||
//参数合法性验证
|
||||
Map<String, String> errors = ValidatorUtils.buildValidationErrorMessageMap(bindingResult);
|
||||
if (!errors.isEmpty()) {
|
||||
return DataPacketUtil.jsonFailResult(errors);
|
||||
}
|
||||
//platform校验
|
||||
ResponseData jsonFailResult = ValidatorUtils.validatePlatformCode(platform);
|
||||
if (jsonFailResult != null) return jsonFailResult;
|
||||
|
||||
|
|
@ -86,15 +80,9 @@ public class NotificationController {
|
|||
public ResponseData changeNotificationStatus(@ApiParam(value = "平台编码", required = true)
|
||||
@PathVariable(name = "platform") String platform,
|
||||
|
||||
@Validated @RequestBody UpdateNotificationStatusParamsVo updateNotificationStatusParamsVo,
|
||||
|
||||
BindingResult bindingResult) {
|
||||
@Validated @RequestBody UpdateNotificationStatusParamsVo updateNotificationStatusParamsVo) {
|
||||
|
||||
//参数合法性验证
|
||||
Map<String, String> errors = ValidatorUtils.buildValidationErrorMessageMap(bindingResult);
|
||||
if (!errors.isEmpty()) {
|
||||
return DataPacketUtil.jsonFailResult(errors);
|
||||
}
|
||||
ResponseData jsonFailResult = ValidatorUtils.validatePlatformCode(platform);
|
||||
if (jsonFailResult != null) return jsonFailResult;
|
||||
|
||||
|
|
@ -117,15 +105,9 @@ public class NotificationController {
|
|||
public ResponseData changeNotificationStatus(@ApiParam(value = "平台编码", required = true)
|
||||
@PathVariable(name = "platform") String platform,
|
||||
|
||||
@Validated @RequestBody DeleteNotificationsVo deleteNotificationsVo,
|
||||
|
||||
BindingResult bindingResult) {
|
||||
@Validated @RequestBody DeleteNotificationsVo deleteNotificationsVo) {
|
||||
|
||||
//参数合法性验证
|
||||
Map<String, String> errors = ValidatorUtils.buildValidationErrorMessageMap(bindingResult);
|
||||
if (!errors.isEmpty()) {
|
||||
return DataPacketUtil.jsonFailResult(errors);
|
||||
}
|
||||
ResponseData jsonFailResult = ValidatorUtils.validatePlatformCode(platform);
|
||||
if (jsonFailResult != null) return jsonFailResult;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue