spring - 在我的 springboot 组件中包含一个库服务或 API
问题描述
有人创建了一个 springboot 组件/应用程序来处理错误并将其变成一个库。
我需要在我的springboot应用程序/组件中包含/调用它,并以下列方式使用它:
无论我在哪里捕获异常,我都应该定义一个错误代码并将其传递给库,然后库应该返回该特定错误代码的描述。
我如何实现这一目标?
图书馆代码如下
控制器:
@Controller
public class ErrorHandlingController extends ResponseEntityExceptionHandler {
@Autowired
private ErrorHandlingService errorHandlingService;
@ResponseStatus
@RequestMapping("err/{errorCode}")
public ResponseEntity<ExceptionDetailsOutputEntity> getErrorByID(@PathVariable int errorCode) throws GenericEntityNotFoundException {
try {
return ResponseEntity.ok(errorHandlingService.retrieveExceptionDetails(errorCode));
} catch (GenericEntityNotFoundException ex) {
ex.printStackTrace();
return ResponseEntity.ok(errorHandlingService.retrieveExceptionDetails(ErrorMessageEnum.Error_not_defined));
}
}
}
@Data
@Entity(name = "ERROR_MESSAGES")
public class ExceptionDetailsOutputEntity implements Serializable {
@Id
private int errorCode;
private String userMessage;
private String message;
......;
}
@Data
public class ErrorLogLayout implements Serializable {
private String applicationName ;
private String serverName ;
.......
...
}
@Service
public interface ErrorHandlingService {
ExceptionDetailsOutputEntity retrieveExceptionDetails(int errorCode) throws GenericEntityNotFoundException;
}
服务实施
@Service
public class ErrorHandlingServiceImpl implements ErrorHandlingService {
@Autowired
ErrorHandlingRepoitory errorHandlingRepoitory;
@Override
public ExceptionDetailsOutputEntity retrieveExceptionDetails(int errorCode) throws GenericEntityNotFoundException {
logger.debug("Inside retrieveExceptionDetails service ");
try {
errorDetailsResponse = errorHandlingRepoitory.findByErrorCode(errorCode);
if(errorDetailsResponse!=null){
if (errorDetailsResponse.getCategory().equalsIgnoreCase(ERROR_CATEGORY)
|| errorDetailsResponse.getCategory().equalsIgnoreCase(ERROR_CAT)) {
errorLoggingMapping(errorDetailsResponse);
}
}else{
retrieveGenericError();
errorLoggingMapping(errorDetailsResponse);
}
return errorDetailsResponse;
} catch (Exception e) {
throw new GenericEntityNotFoundException(e.getMessage());
}
}
public void writeToaFile(ErrorLogLayout layout) throws IOException {
File filePath = new File("/opt/ErrorLoggingLogs/ ");
FileWriter fr = new FileWriter(filePath + "error.log" + "." + getDateFormat(), true);
BufferedWriter out = new BufferedWriter(fr);
out.write(String.valueOf(layout));
out.newLine();
out.close();
fr.close();
}
public void errorLoggingMapping(ExceptionDetailsOutputEntity errorDetailsResponse) throws GenericEntityNotFoundException {
try {
ErrorLogLayout layout = new ErrorLogLayout();
layout.setErrorCode(errorDetailsResponse.getErrorCode());
.....
...
}catch(IOException io){
throw new GenericEntityNotFoundException(io.getMessage());
}
}
private String getDateFormat(){
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd") ;
String curDate = dateFormat.format(date);
return curDate;
}
private void retrieveGenericError(){
errorDetailsResponse = new ExceptionDetailsOutputEntity();
errorDetailsResponse.setErrorCode(ErrorMessageEnum.Error_not_defined);
.....
...
}
}
解决方案
推荐阅读
- sapui5 - 如何从对话框中选择的下拉列表中将数据添加到表行
- facebook - Facebook 错误代码:4 | 消息:已达到应用程序请求限制
- java - 通过在 Java/Python 中从照片中删除黑色边框,从图像中裁剪精确的文档纸
- python - 如何在类中使用子进程并在 python 中进行测试
- ms-word - Office URI 方案,来自模板(nft)的新文件未将文件保存在提供的位置
- rust - 将结果的错误代码转换为字符串
- netlogo - 有没有办法在 netlogo 中生成 .nls 文件
- cassandra - 在 cassandra cql 中获取用户消息的最佳方式?
- xamarin.forms - xamarin 表单中的水平分组列表视图
- tensorflow - 在 Keras 上运行 VGG-19 和在 11GB GPU 上运行 tensorflow 内存不足