<?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>Forem: Josel099</title>
    <description>The latest articles on Forem by Josel099 (@josel099).</description>
    <link>https://forem.com/josel099</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%2F970823%2F4b10e926-5df9-4a8e-8224-0203e33c0967.jpeg</url>
      <title>Forem: Josel099</title>
      <link>https://forem.com/josel099</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/josel099"/>
    <language>en</language>
    <item>
      <title>Swagger Made Simple: A Walkthrough of API Documentation</title>
      <dc:creator>Josel099</dc:creator>
      <pubDate>Sun, 20 Oct 2024 08:04:47 +0000</pubDate>
      <link>https://forem.com/josel099/swagger-made-simple-a-walkthrough-of-api-documentation-3n4k</link>
      <guid>https://forem.com/josel099/swagger-made-simple-a-walkthrough-of-api-documentation-3n4k</guid>
      <description>&lt;p&gt;In  software development , APIs enable communication between different applications, acting as a bridge between systems. Without proper documentation, APIs are hard to use.&lt;/p&gt;

&lt;p&gt;Backend developers create APIs, and frontend developers integrate them into the UI. To do this, they need &lt;strong&gt;API specifications&lt;/strong&gt;. API documentation is a guide that explains how to use the API, including making requests, sending data, handling responses, and errors.&lt;/p&gt;

&lt;h5&gt;
  
  
  The API documentation helps developers:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;Understand the available endpoints and their functions.&lt;/li&gt;
&lt;li&gt;Know the request parameters and data formats. &lt;/li&gt;
&lt;li&gt;Properly handle responses and errors.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here's where &lt;strong&gt;Swagger&lt;/strong&gt; is useful. It's part of an  &lt;strong&gt;openAPI Intiative&lt;/strong&gt;. Swagger is a set of open-source tools built around the &lt;strong&gt;OpenAPI Specification&lt;/strong&gt; that can help you design, build, document, and consume REST APIs.&lt;/p&gt;

&lt;h2&gt;
  
  
  OpenAPI
&lt;/h2&gt;

&lt;h4&gt;
  
  
  What is OpenAPI Specifications ?
&lt;/h4&gt;

&lt;p&gt;The &lt;strong&gt;OpenAPI Specification&lt;/strong&gt; (OAS) is used to define the details of REST API's, like the available endpoints, how requests and responses should be formatted, what parameters are needed, and how authentication works. Normally, OpenAPI descriptions are written in either &lt;em&gt;YAML&lt;/em&gt; or &lt;em&gt;JSON&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Building Blocks of Open API specifications
&lt;/h4&gt;

&lt;p&gt;In the diagram below you can see an overview of the building blocks of the OpenAPI Specification. The green boxes are the &lt;em&gt;mandatory&lt;/em&gt; fields and the orange ones are &lt;em&gt;optional&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2514ynloyrzrl4lq4rfs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2514ynloyrzrl4lq4rfs.png" alt="This diagram illustrates the 8 essential building blocks of an OpenAPI specification. It categorizes the fundamental elements needed to define a RESTful API clearly and effectively" width="560" height="1800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1.openapi:&lt;/strong&gt; The version of the OpenAPI specification being used (mandatory).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2.info:&lt;/strong&gt; Information about the API, like title, version, and description (mandatory).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3.servers:&lt;/strong&gt; Defines the servers where the API is hosted (optional).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4.paths:&lt;/strong&gt; Lists the available API endpoints and their operations (mandatory).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5.components:&lt;/strong&gt; Holds various reusable components, such as schemas  (data models) , parameters etc (optional).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6.security:&lt;/strong&gt; Defines authentication mechanisms for the API, like API keys, OAuth, or HTTP-based auth (optional).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7.tags:&lt;/strong&gt; The tags section is used to group operations logically for better organization(optional).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8.externalDocs:&lt;/strong&gt; Allows you to include links to external documentation for additional information on specific parts of the API. (optional).&lt;/p&gt;

&lt;h2&gt;
  
  
  SWAGGER
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Let's take a look at the history of Swagger.
&lt;/h4&gt;

&lt;p&gt;Swagger originally started as an open-source project in 2010 by &lt;strong&gt;SmartBear Software&lt;/strong&gt; .In 2015, it was contributed to the &lt;strong&gt;OpenAPI Initiative&lt;/strong&gt;. This initiative is backed by IT big giant's  like &lt;em&gt;Amazon&lt;/em&gt;, &lt;em&gt;Google&lt;/em&gt;, and the &lt;em&gt;Linux Foundation&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Today, APIs are developed using &lt;strong&gt;OpenAPI specifications&lt;/strong&gt;, and &lt;strong&gt;Swagger&lt;/strong&gt; is a toolset used for designing, building, and documenting APIs that follow the &lt;em&gt;OpenAPI standards&lt;/em&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Let's meet some swagger tools ...
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;1. Swagger UI&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;Swagger UI is a web tool that automatically creates interactive API documentation. It allow developers and users test the API by sending requests and seeing the results directly in the browser. Think of it as a &lt;em&gt;live manual&lt;/em&gt; for your API.&lt;/p&gt;

&lt;p&gt;Check out this swagger live &lt;a href="https://petstore.swagger.io/#/" rel="noopener noreferrer"&gt;&lt;em&gt;demo&lt;/em&gt;&lt;/a&gt;. You can understand it better by interacting with it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://petstore.swagger.io/#/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6vitb9nd4njsv4yf8wx3.png" alt="The image showcases Swagger UI, a web-based tool that dynamically generates interactive API documentation." width="800" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Swagger Editor&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Swagger Editor is a browser tool where you can write or edit API specifications using simple text (in YAML or JSON format). It helps you to create the API documentation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://editor.swagger.io/" rel="noopener noreferrer"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frx56xw3igfjysc6tza3d.png" alt="Swagger Editor, a web-based tool where you can write and edit API specifications in YAML or JSON format. " width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Swagger Codegen&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://swagger.io/tools/swagger-codegen/" rel="noopener noreferrer"&gt;Swagger Codegen&lt;/a&gt;  is a tool that generates code from your API specifications. It creates templates for server or client apps in various languages, saving time by providing a base for API integration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. SwaggerHub&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://swagger.io/tools/swaggerhub/" rel="noopener noreferrer"&gt;SwaggerHub&lt;/a&gt; is a collaborative platform for designing, building, and managing APIs using the OpenAPI Specification.  It combines tools like Swagger UI, Editor, and Codegen in one place,making it easier for teams to work together on API projects.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;It's better to think of Swagger as similar to Postman or Hoppscotch, but Swagger primarily focuses on API design and documentation, while Postman and Hoppscotch are more focused on API testing, development, and debugging.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Integrate Swagger in Your Spring boot Project.
&lt;/h2&gt;

&lt;p&gt;You can integrate Swagger into a Spring Boot project using either &lt;strong&gt;&lt;a href="https://github.com/springfox/springfox" rel="noopener noreferrer"&gt;Springfox&lt;/a&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;a href="https://springdoc.org/" rel="noopener noreferrer"&gt;Springdoc&lt;/a&gt;&lt;/strong&gt;. However, it appears that the Springfox project is no longer actively maintained, with no commits in the last four years and no updates to accommodate newer technologies like &lt;em&gt;Jakarta EE&lt;/em&gt; or &lt;em&gt;Spring Boot 3.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We can use &lt;strong&gt;Springdoc&lt;/strong&gt;, which I think is easier to set up.&lt;/p&gt;

&lt;p&gt;To integrate Swagger into your &lt;em&gt;Spring Boot project&lt;/em&gt;, add this dependency to your &lt;strong&gt;pom.xml&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;
    &amp;lt;groupId&amp;gt;org.springdoc&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;springdoc-openapi-ui&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;1.6.6&amp;lt;/version&amp;gt; &amp;lt;!-- Replace with the latest version --&amp;gt;
&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After that, you can access the Swagger UI at :&lt;br&gt;
&lt;a href="http://localhost:8080/swagger-ui/index.html" rel="noopener noreferrer"&gt;http://localhost:8080/swagger-ui/index.html&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the UI, you can view all your endpoints, schemas, and API information. To see the API documentation in JSON format, visit :&lt;br&gt;
&lt;a href="http://localhost:8080/v3/api-docs" rel="noopener noreferrer"&gt;http://localhost:8080/v3/api-docs&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For advanced documentation, &lt;strong&gt;Springdoc&lt;/strong&gt; provides a variety of useful annotations. Let's take a look at some of them:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. @Operation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This annotation is used to document individual API operations. It provides information like the summary, description, and response types.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Operation(
    summary = "Get all Employees",
    description = "Returns a list of all Employees”
)
    @GetMapping
    public List&amp;lt;Employee&amp;gt; getAllEmployee(){
        return employeeService.getAllEmployee();
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F97df03iqf0hlnhnw6csq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F97df03iqf0hlnhnw6csq.png" alt="This image displays an example of a GET request to the /employee endpoint. The operation is documented with a summary (" width="800" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. @ApiResponses and @ApiResponse&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These annotations are used to specify possible HTTP responses for an operation, including different response codes and descriptions.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; @DeleteMapping("/{id}")
    @ApiResponses(value = { @ApiResponse(responseCode = "200", description = "Employee Deleted"), @ApiResponse(responseCode = "404", description = "Employee not found") })
    public void deleteEmployee(@PathVariable int id){
        employeeService.deleteEmployee(id);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphu0ie74laekecppzeex.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fphu0ie74laekecppzeex.png" alt="This image shows a DELETE request for /employee/{id} that deletes a specific employee. The responses include 200 (Employee Deleted) and 404 (Employee not found)." width="800" height="275"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. @Parameter&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This annotation is used to describe a specific request parameter or path variable. You can provide details such as whether the parameter is required or optional, along with a description of its purpose.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@GetMapping("/{id}")
public Optional&amp;lt;Employee&amp;gt; getEmployeeById(
    @Parameter(description = "ID of the Employee to be retrieved", required = true)
    @PathVariable int id){
    return employeeService.getEmployeeById(id);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2nwq6f2zj5i589tf0cal.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2nwq6f2zj5i589tf0cal.png" alt="This image shows a GET request for /employee/{id}, with a description for the required path variable id, which is the ID of the employee to be retrieved" width="800" height="135"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. @Tag&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Used to group related operations in the documentation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Tag(name = "Employees", description = "Operations related to Employees")
@RestController
@RequestMapping("/employee")
public class EmployeeController {
- - - - - - - - - - 
- - - - - - - - - - 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wl4ntd04s3k2ezw6aet.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wl4ntd04s3k2ezw6aet.png" alt="This image shows a list of requests grouped under a common @Tag annotation." width="800" height="166"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Resources
&lt;/h2&gt;

&lt;p&gt;For readers who want to dive deeper into Swagger and OpenAPI, here are some valuable resources:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://swagger.io/docs/" rel="noopener noreferrer"&gt;Swagger Official Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.0.3.md#openapi-document" rel="noopener noreferrer"&gt;OpenAPI Specification(version 3.1.0)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://springdoc.org/" rel="noopener noreferrer"&gt;Springdoc OpenAPI &lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://testsigma.com/blog/swagger-vs-postman/" rel="noopener noreferrer"&gt;Postman vs. Swagger &lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are some of the resources I referred to. If you have any questions or corrections, please let me know in the comments section.&lt;/p&gt;

</description>
      <category>api</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>springboot</category>
    </item>
    <item>
      <title>Hacktober Maintainer -2023</title>
      <dc:creator>Josel099</dc:creator>
      <pubDate>Sun, 12 Nov 2023 17:49:16 +0000</pubDate>
      <link>https://forem.com/josel099/hacktober-maintainer-2023-202p</link>
      <guid>https://forem.com/josel099/hacktober-maintainer-2023-202p</guid>
      <description>&lt;p&gt;As a proud participant in &lt;strong&gt;Hacktoberfest&lt;/strong&gt;, I am excited to contribute as a maintainer to open-source projects. Being a maintainer during Hacktoberfest is not just about receiving contributions but fostering a collaborative community. I welcome developers to join me in making impactful contributions, whether it's fixing bugs, adding new features, or improving documentation. Together, we can make a difference in the open-source landscape. Join me on this journey of collaboration, innovation, and giving back to the community!&lt;/p&gt;

</description>
      <category>hack23maintainer</category>
    </item>
    <item>
      <title>Hacktober Contributor</title>
      <dc:creator>Josel099</dc:creator>
      <pubDate>Sun, 12 Nov 2023 17:44:20 +0000</pubDate>
      <link>https://forem.com/josel099/placeholder-contributor-5e4d</link>
      <guid>https://forem.com/josel099/placeholder-contributor-5e4d</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hacktoberfest 2023 Contributor&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I am thrilled to have actively participated in Hacktoberfest 2023, contributing to open-source projects and engaging with the global developer community. During this exciting event, I collaborated on diverse projects, enhancing my skills and making meaningful contributions to the world of open source. It was an enriching experience, and I look forward to continuing my journey of learning and collaboration in the vibrant open-source community.&lt;/p&gt;

</description>
      <category>hack23contributor</category>
    </item>
  </channel>
</rss>
