diff --git a/radar-engine/src/main/java/com/pgmmers/radar/controller/EventRequest.java b/radar-engine/src/main/java/com/pgmmers/radar/controller/EventRequest.java new file mode 100644 index 0000000..ce9626e --- /dev/null +++ b/radar-engine/src/main/java/com/pgmmers/radar/controller/EventRequest.java @@ -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; + } +} diff --git a/radar-engine/src/main/java/com/pgmmers/radar/controller/MainController.java b/radar-engine/src/main/java/com/pgmmers/radar/controller/MainController.java index 30a2967..0a4edb7 100644 --- a/radar-engine/src/main/java/com/pgmmers/radar/controller/MainController.java +++ b/radar-engine/src/main/java/com/pgmmers/radar/controller/MainController.java @@ -19,6 +19,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import javax.validation.Valid; + @RestController @RequestMapping("/services/v1") @Api(value = "RiskApi", description = "接受用户事件数据,实时进行分析并返回分析结果。", tags = {"风险分析API(引擎端)"}) @@ -28,6 +30,7 @@ public class MainController { private RiskAnalysisEngineService engineApi; + @Deprecated @PostMapping("/uploadInfo") @ApiOperation(value = "事件数据提交接口") public CommonResult upload(@RequestParam @ApiParam(name="modelGuid", value="模型Guid", required=true) String modelGuid, @@ -44,4 +47,11 @@ public CommonResult getScore(@RequestParam @ApiParam(name="modelGuid",value=" CommonResult result = engineApi.getScore(modelGuid, reqId); return result; } + + @PostMapping("/upload") + @ApiOperation(value = "事件数据提交接口") + public CommonResult upload(@Valid @RequestBody EventRequest request) { + CommonResult result = engineApi.uploadInfo(request.getGuid(), request.getReqId(), request.getJsonInfo()); + return result; + } } diff --git a/radar-engine/src/main/java/com/pgmmers/radar/error/GlobalExceptionHandler.java b/radar-engine/src/main/java/com/pgmmers/radar/error/GlobalExceptionHandler.java new file mode 100644 index 0000000..a2a0243 --- /dev/null +++ b/radar-engine/src/main/java/com/pgmmers/radar/error/GlobalExceptionHandler.java @@ -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 errorList = new ArrayList<>(); + if (bindingResult.hasErrors()) { + List allErrors = bindingResult.getAllErrors(); + for (ObjectError objectError : allErrors) { + String message = objectError.getDefaultMessage(); + errorList.add(message); + } + } + result.getData().put("errList", errorList); + return result; + } +} \ No newline at end of file diff --git a/radar-service-impl/src/main/java/com/pgmmers/radar/service/impl/core/RiskAnalysisEngineServiceImpl.java b/radar-service-impl/src/main/java/com/pgmmers/radar/service/impl/core/RiskAnalysisEngineServiceImpl.java index ecce4af..e04ae7b 100644 --- a/radar-service-impl/src/main/java/com/pgmmers/radar/service/impl/core/RiskAnalysisEngineServiceImpl.java +++ b/radar-service-impl/src/main/java/com/pgmmers/radar/service/impl/core/RiskAnalysisEngineServiceImpl.java @@ -58,13 +58,18 @@ public RiskAnalysisEngineServiceImpl( @Override public CommonResult uploadInfo(String modelGuid, String reqId, String jsonInfo) { + return uploadInfo(modelGuid, reqId, JSON.parseObject(jsonInfo)); + } + + @Override + public CommonResult uploadInfo(String modelGuid, String reqId, JSONObject jsonInfo) { logger.info("req info:{},{},{}", modelGuid, reqId, jsonInfo); CommonResult result = new CommonResult(); Map> context = new HashMap<>(); ModelVO model; try { // 1. check - JSONObject eventJson = JSON.parseObject(jsonInfo); + JSONObject eventJson = jsonInfo; model = modelService.getModelByGuid(modelGuid); @@ -110,8 +115,9 @@ public CommonResult uploadInfo(String modelGuid, String reqId, String jsonInfo) .formatDate(new Date(eventTimeMillis), "yyyy-MM-dd'T'HH:mm:ssZ"); preItemMap.put("radar_ref_datetime", timeStr); } catch (Exception e) { - e.printStackTrace(); - result.setMsg("数据异常!"); + logger.error("process error", e); + //result.setMsg("数据异常!" + e.getMessage()); + throw new RuntimeException("数据处理异常:" + e.getMessage()); } // 缓存分析结果 @@ -135,4 +141,5 @@ public CommonResult getScore(String modelGuid, String reqId) { return result; } + } diff --git a/radar-service/src/main/java/com/pgmmers/radar/service/RiskAnalysisEngineService.java b/radar-service/src/main/java/com/pgmmers/radar/service/RiskAnalysisEngineService.java index d567ec0..610f66e 100644 --- a/radar-service/src/main/java/com/pgmmers/radar/service/RiskAnalysisEngineService.java +++ b/radar-service/src/main/java/com/pgmmers/radar/service/RiskAnalysisEngineService.java @@ -1,6 +1,7 @@ package com.pgmmers.radar.service; +import com.alibaba.fastjson.JSONObject; import com.pgmmers.radar.service.common.CommonResult; @@ -24,9 +25,17 @@ public interface RiskAnalysisEngineService { * @author feihu.wang * */ - CommonResult uploadInfo( String modelGuid, - String reqId, - String jsonInfo); + CommonResult uploadInfo(String modelGuid, String reqId, String jsonInfo); + + /** + * 上传信息. + * @param modelGuid + * @param reqId + * @param jsonInfo + * @return + * @see "uploadInfo(String modelGuid, String reqId, String jsonInfo)" + */ + CommonResult uploadInfo(String modelGuid, String reqId, JSONObject jsonInfo); /** *