Skip to content

Commit

Permalink
opts: 代码优化.
Browse files Browse the repository at this point in the history
feihu wang
  • Loading branch information
feihu.wang committed Mar 12, 2021
1 parent ff5330d commit d973738
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 6 deletions.
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(引擎端)"})
Expand All @@ -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,
Expand All @@ -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;
}
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Map<String, ?>> context = new HashMap<>();
ModelVO model;
try {
// 1. check
JSONObject eventJson = JSON.parseObject(jsonInfo);
JSONObject eventJson = jsonInfo;

model = modelService.getModelByGuid(modelGuid);

Expand Down Expand Up @@ -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());
}

// 缓存分析结果
Expand All @@ -135,4 +141,5 @@ public CommonResult getScore(String modelGuid, String reqId) {
return result;
}


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pgmmers.radar.service;


import com.alibaba.fastjson.JSONObject;
import com.pgmmers.radar.service.common.CommonResult;


Expand All @@ -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);

/**
*
Expand Down

0 comments on commit d973738

Please sign in to comment.