首页 > 解决方案 > 在我的 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);
        .....
        ...
    }
}

标签: springspring-bootspring-mvcspring-data-jpa

解决方案


推荐阅读