title: Include a library service or API into my springboot component
body: <p>Someone has created a springboot component/application to handle errors and turned it into a library.</p>
  
  <p>I need to include/call it in my springboot application/component and use it in the following way:</p>
  
  <p>wherever i catch an exception i should define an error code and pass it to the library and then the library should return me the description of that specific error code.</p>
  
  <p>How do i achieve this? </p>
  
  <p>The library code is if below</p>
  
  <p>Controller:</p>
  
  <pre><code>
  @Controller
  public class ErrorHandlingController extends ResponseEntityExceptionHandler {
  
      @Autowired
      private ErrorHandlingService errorHandlingService;
  
      @ResponseStatus
      @RequestMapping("err/{errorCode}")
      public ResponseEntity&lt;ExceptionDetailsOutputEntity&gt; 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));
          }
      }
  }
  
  </code></pre>
  
  <pre><code>@Data
  @Entity(name = "ERROR_MESSAGES")
  public class ExceptionDetailsOutputEntity implements Serializable {
  
      @Id
      private int errorCode;
      private String userMessage;
      private String message;
      ......;
  
  }
  </code></pre>
  
  <pre><code>
  @Data
  public class ErrorLogLayout implements Serializable {
  
      private  String applicationName ;
      private  String serverName ;
      .......
      ...
  
  }
  </code></pre>
  
  <pre><code>
  @Service
  public interface ErrorHandlingService {
  
      ExceptionDetailsOutputEntity retrieveExceptionDetails(int errorCode) throws GenericEntityNotFoundException;
  }
  
  </code></pre>
  
  <p>Service implementation</p>
  
  <pre><code>@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);
          .....
          ...
      }
  }
  </code></pre>