<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: DzifaHodey</title>
    <description>The latest articles on DEV Community by DzifaHodey (@dzifahodey).</description>
    <link>https://dev.to/dzifahodey</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F613325%2F6888c1a0-b21f-4f21-9f06-979b9017c910.png</url>
      <title>DEV Community: DzifaHodey</title>
      <link>https://dev.to/dzifahodey</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dzifahodey"/>
    <language>en</language>
    <item>
      <title>NoSuchFileException - How to avoid it when adding resources to a Spring Boot Application</title>
      <dc:creator>DzifaHodey</dc:creator>
      <pubDate>Wed, 18 May 2022 14:21:17 +0000</pubDate>
      <link>https://dev.to/dzifahodey/the-java-nosuchfileexception-how-to-avoid-it-when-adding-resources-to-a-spring-boot-application-2bd6</link>
      <guid>https://dev.to/dzifahodey/the-java-nosuchfileexception-how-to-avoid-it-when-adding-resources-to-a-spring-boot-application-2bd6</guid>
      <description>&lt;p&gt;Have you ever tried reading resources (json files, htm templates, text files etc) in your Spring Boot project, but got a java.nio.file.NoSuchFileException?&lt;br&gt;
&lt;a href="https://i.giphy.com/media/IejsMRUQLlphOfMmI1/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/IejsMRUQLlphOfMmI1/giphy.gif" alt="confused" width="480" height="360"&gt;&lt;/a&gt; &lt;br&gt;
The &lt;em&gt;NoSuchFileException&lt;/em&gt; occurs if the file is not in the specified location. A similar exception that occurs is the &lt;em&gt;FileNotFoundException&lt;/em&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Loading Files from a directory
&lt;/h3&gt;

&lt;p&gt;A common approach developers use to add resources is to place the files in the &lt;em&gt;src/main/resources/&lt;/em&gt; directory, and then read the files from that path.&lt;br&gt;
Although not the best practice, this approach works if the project is run locally. This is because the project directory is used as the current working directory during runtime.&lt;/p&gt;

&lt;p&gt;For example, using the code below, I can read user data from a file and save the value in a string. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;application.properties&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight properties"&gt;&lt;code&gt;&lt;span class="py"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;src/main/resources/filename.txt&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;ReadUserDataFromFile.java&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReadUserDataFromFile&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"${filePath}"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;dataFilePath&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;readDataFile&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Files&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;readAllBytes&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Paths&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;get&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;
     &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The file is successfully read if it is in the &lt;em&gt;src/main/resources/&lt;/em&gt; directory.&lt;/p&gt;

&lt;p&gt;Now, if the project is packaged as a JAR file, the &lt;em&gt;NoSuchFileException&lt;/em&gt; will be thrown when trying to read the file. This happens because &lt;em&gt;filename.txt&lt;/em&gt; is at the root folder of the JAR and it cannot be accessed using the file path. &lt;/p&gt;

&lt;h3&gt;
  
  
  2. Creating a Resource object
&lt;/h3&gt;

&lt;p&gt;A better approach and a solution to the &lt;em&gt;NoSuchFileException&lt;/em&gt; is to create a org.springframework.core.io.Resource object in your class and set the value to point to the file.&lt;br&gt;
Example:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;ReadUserDataFromFile.java&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReadUserDataFromFile&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
   &lt;span class="nd"&gt;@Value&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"classpath:filename.txt"&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt;
   &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Resource&lt;/span&gt; &lt;span class="n"&gt;dataFile&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

   &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;readDataFile&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dataFile&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInputStream&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;readAllBytes&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this approach, the file can be read both locally and from JAR file.&lt;/p&gt;

</description>
      <category>java</category>
      <category>spring</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Welcoming myself to the tech blogging space</title>
      <dc:creator>DzifaHodey</dc:creator>
      <pubDate>Wed, 20 Apr 2022 18:09:02 +0000</pubDate>
      <link>https://dev.to/dzifahodey/welcoming-myself-to-the-tech-blogging-space-2p56</link>
      <guid>https://dev.to/dzifahodey/welcoming-myself-to-the-tech-blogging-space-2p56</guid>
      <description>&lt;p&gt;I am very excited to start documenting my software development journey. My stack is mainly Java Spring Boot and Python Flask for API Development. &lt;br&gt;
My posts will be centered on general software engineering practices as well as Spring Boot concepts.&lt;/p&gt;

&lt;p&gt;Cheers to my tech journey!&lt;br&gt;
&lt;a href="https://i.giphy.com/media/29wik0hT7Ck9jppi6D/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/29wik0hT7Ck9jppi6D/giphy.gif" alt="Cheers" width="480" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Creating my first ASP.NET Core Web API</title>
      <dc:creator>DzifaHodey</dc:creator>
      <pubDate>Wed, 21 Apr 2021 16:25:59 +0000</pubDate>
      <link>https://dev.to/dzifahodey/creating-my-first-asp-net-core-web-api-22i4</link>
      <guid>https://dev.to/dzifahodey/creating-my-first-asp-net-core-web-api-22i4</guid>
      <description>&lt;p&gt;As an Electrical &amp;amp; Electronic Engineering graduate, I finally decided to begin a new journey in the software development world. Although I have beginner knowledge of a few programming languages, frameworks and popular software engineering practices, I would say my learning was not structured.&lt;/p&gt;

&lt;p&gt;My first step in this new journey is to learn ASP.NET Core development, and I'm eager to see what it has to offer! In this post, I will describe my first experience with the ASP.NET Core framework using the &lt;a href="https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&amp;amp;tabs=visual-studio"&gt;Microsoft Docs tutorial&lt;/a&gt;. I have also included links to key concepts and resources.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--F9nepeSl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://stevewasserman.co.uk/wp-content/uploads/2020/05/1_yzgJ-66YvsokIvqw93jiJQ-300x128.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--F9nepeSl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/http://stevewasserman.co.uk/wp-content/uploads/2020/05/1_yzgJ-66YvsokIvqw93jiJQ-300x128.png" alt="Why" title="Why?"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  First of all, why ASP.NET Core?
&lt;/h3&gt;

&lt;p&gt;ASP.NET Core is a web development framework based on C# that supports multiple platforms such as Windows, Linux and macOS. Being an open source framework, it is very flexible to work with.&lt;br&gt;
This framework features an &lt;a href="https://www.tutorialspoint.com/mvc_framework/mvc_framework_introduction.htm"&gt;MVC architecture&lt;/a&gt;; Model, View, Controller; which makes it really easy to customize web applications and test APIs &lt;a href="https://careers.upwork.com/blogs/startup-resources-upwork-blog/what-is-an-api-and-how-does-it-work"&gt;(Application Programming Interfaces)&lt;/a&gt;. APIs are gateways that enable applications communicate with each other. &lt;br&gt;
As I keep working with the framework, I hope to learn more about its usefulness and advantages over other frameworks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Now, to the process!
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/YAnpMSHcurJVS/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/YAnpMSHcurJVS/giphy.gif" alt="Typing"&gt;&lt;/a&gt;&lt;br&gt;
After installing the .NET &lt;a href="https://clevertap.com/blog/what-is-an-sdk/"&gt;Software Development Kit&lt;/a&gt; (SDK) and getting my Visual Studio Code ready with the C# extension, I was set to start.&lt;br&gt;
Using the integrated terminal in Visual Studio Code, I created a new web API project. One thing I liked was that the new project came with a template that helped give a clearer understanding of how the API worked and was structured.&lt;/p&gt;

&lt;p&gt;To develop my API, which was a to-do list tracker,  I created my database model and a database context that was set up to use an in-memory database. I then scaffolded the controller to enable it respond to web API requests using &lt;a href="https://www.codecademy.com/articles/what-is-crud"&gt;CRUD&lt;/a&gt; (Create, Read, Update and Delete) operations quickly and asynchronously. &lt;a href="https://www.dotnettricks.com/learn/mvc/understanding-aspnet-mvc-scaffolding"&gt;Scaffolding&lt;/a&gt; is a technique used to generate code for CRUD methods to be used to GET, POST, PUT and DELETE data in the database efficiently. &lt;br&gt;
I included a &lt;a href="https://www.infoworld.com/article/3562271/how-to-use-data-transfer-objects-in-aspnet-core-31.html"&gt;Data Transfer Object&lt;/a&gt; (DTO) model which controls the properties that are returned to the client, and prevents over-posting. Apart from security, the DTO is useful for reducing payload size.&lt;/p&gt;

&lt;p&gt;I thoroughly enjoyed the process of creating my first .NET CORE project, and I'm looking forward to exploring its functionality even more.&lt;br&gt;
In my subsequent tasks, I will be optimizing the performance of the API.&lt;br&gt;
Hope you enjoyed this read.! Like and comment :).&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
