-
Notifications
You must be signed in to change notification settings - Fork 487
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feihu wang
- Loading branch information
feihu.wang
committed
Mar 12, 2021
1 parent
ff5330d
commit d973738
Showing
5 changed files
with
152 additions
and
6 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
radar-engine/src/main/java/com/pgmmers/radar/controller/EventRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.pgmmers.radar.controller; | ||
|
||
import com.alibaba.fastjson.JSONObject; | ||
import io.swagger.annotations.ApiModelProperty; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
import javax.validation.constraints.NotNull; | ||
|
||
/** | ||
* 事件信息。 | ||
* @author feihu.wang | ||
*/ | ||
public class EventRequest { | ||
@ApiModelProperty(value = "模型guid") | ||
@NotBlank(message = "guid 不能为空") | ||
private String guid; | ||
|
||
@ApiModelProperty(value = "请求流水号") | ||
@NotBlank(message = "reqId 不能为空") | ||
private String reqId; | ||
|
||
@ApiModelProperty(value = "事件内容") | ||
@NotNull(message = "jsonInfo 不能为空") | ||
private JSONObject jsonInfo; | ||
|
||
public String getGuid() { | ||
return guid; | ||
} | ||
|
||
public void setGuid(String guid) { | ||
this.guid = guid; | ||
} | ||
|
||
public String getReqId() { | ||
return reqId; | ||
} | ||
|
||
public void setReqId(String reqId) { | ||
this.reqId = reqId; | ||
} | ||
|
||
public JSONObject getJsonInfo() { | ||
return jsonInfo; | ||
} | ||
|
||
public void setJsonInfo(JSONObject jsonInfo) { | ||
this.jsonInfo = jsonInfo; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
radar-engine/src/main/java/com/pgmmers/radar/error/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.pgmmers.radar.error; | ||
|
||
import com.pgmmers.radar.service.common.CommonResult; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.http.converter.HttpMessageConversionException; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.ObjectError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ControllerAdvice; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.ResponseBody; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* global exception handle. | ||
* @author feihu.wang | ||
*/ | ||
@ControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
@ResponseBody | ||
public ResponseEntity handleMethodArgumentNotValidException( | ||
MethodArgumentNotValidException e) { | ||
logger.error("handleMethodArgumentNotValidException: ", e.getMessage()); | ||
CommonResult result = handleBindingResult(e.getBindingResult()); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result); | ||
} | ||
|
||
@ExceptionHandler(RuntimeException.class) | ||
@ResponseBody | ||
public ResponseEntity handleRuntimeException(RuntimeException e) { | ||
logger.error("handleRuntimeException: ", e); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(HttpMessageConversionException.class) | ||
@ResponseBody | ||
public ResponseEntity handleHttpMessageConversionException(HttpMessageConversionException e) { | ||
logger.error("handleHttpMessageConversionException: ", e); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
@ResponseBody | ||
public ResponseEntity handleException(Exception e) { | ||
logger.error("Exception: ", e); | ||
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); | ||
} | ||
|
||
private CommonResult handleBindingResult(BindingResult bindingResult) { | ||
CommonResult result = new CommonResult(); | ||
List<String> errorList = new ArrayList<>(); | ||
if (bindingResult.hasErrors()) { | ||
List<ObjectError> allErrors = bindingResult.getAllErrors(); | ||
for (ObjectError objectError : allErrors) { | ||
String message = objectError.getDefaultMessage(); | ||
errorList.add(message); | ||
} | ||
} | ||
result.getData().put("errList", errorList); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters