Swagger UI

 To create a Swagger application in Java using Swagger UI, you typically integrate Swagger with a Java framework such as Spring Boot. Swagger UI allows you to visualize and interact with your API's resources using a web interface, making API documentation and testing straightforward. Here’s a step-by-step guide to set up a simple Swagger-enabled Spring Boot application:

Step 1: Set Up a Spring Boot Project

  1. Create a new Spring Boot project using Spring Initializr with the following dependencies:

    • Web
    • Spring Boot DevTools
    • Spring Boot Actuator (optional, for monitoring endpoints)
  2. Add dependencies for Swagger:

    • springfox-swagger2: Swagger core library for API documentation.
    • springfox-swagger-ui: Swagger UI for visualizing and interacting with API resources.

Step 2: Configure Swagger in Spring Boot

  1. Modify pom.xml to include dependencies:


    <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>3.0.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>3.0.0</version> </dependency>
  2. Create a Swagger configuration class (SwaggerConfig.java):


    import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo")) // Specify base package for controllers .paths(PathSelectors.any()) .build(); } }

Step 3: Create a Sample Controller

Create a sample REST controller (SampleController.java) to expose endpoints:


import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api") public class SampleController { @GetMapping("/hello") public String hello() { return "Hello, Swagger!"; } }

Step 4: Run the Application

Run your Spring Boot application. Swagger UI should be accessible at http://localhost:8080/swagger-ui.html. You can interact with your SampleController endpoint (/api/hello) through Swagger UI.

Step 5: Access Swagger UI

Navigate to http://localhost:8080/swagger-ui.html in your web browser. You should see Swagger UI loaded with your API documentation. Explore endpoints, send requests, and view responses directly from the Swagger UI interface.

Daily Knowledge Journey: A Quest for Learning

Object Class

 The Object class in Java is the root of the class hierarchy and serves as the superclass for all other classes. It provides fundamental me...