opts: 代码优化.

feihu wang
This commit is contained in:
feihu.wang 2021-03-12 18:56:11 +08:00
parent ff5330d470
commit d973738fc0
5 changed files with 152 additions and 6 deletions

View File

@ -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;
}
}

View File

@ -19,6 +19,8 @@ import io.swagger.annotations.ApiParam;
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 class MainController {
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;
}
}

View File

@ -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;
}
}

View File

@ -58,13 +58,18 @@ public class RiskAnalysisEngineServiceImpl implements RiskAnalysisEngineService
@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<String, Map<String, ?>> 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 class RiskAnalysisEngineServiceImpl implements RiskAnalysisEngineService
.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 class RiskAnalysisEngineServiceImpl implements RiskAnalysisEngineService
return result;
}
}

View File

@ -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);
/**
*